题解 | 密码验证合格程序

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

package main

import (
	"fmt"
)

func main() {
	var password string
	for true {
		if _, err := fmt.Scan(&password); err != nil {
			break
		}
		// 1、长度不少于8位
		if len(password) < 8 {
			fmt.Println("NG")
			continue
		}
		// 2、必须要包含三种以上的字符
		keyTypeMap := make(map[string]int)
		for _, key := range password {
			if key >= '0' && key <= '9' {
				keyTypeMap["number"] = 1
			} else if key >= 'A' && key <= 'Z' {
				keyTypeMap["upLetter"] = 1
			} else if key >= 'a' && key <= 'z' {
				keyTypeMap["loLetter"] = 1
			} else {
				keyTypeMap["symbol"] = 1
			}
		}
		// fmt.Println(keyTypeMap)
		typeCount := 0
		for _, typeV := range keyTypeMap {
			if typeV == 1 {
				typeCount++
			}
		}
		if typeCount < 3 {
			fmt.Println("NG")
			continue
		}
		// 3、不能出现重复子串
		// todo: 思路:做一个3个字符的滑动块,作为map的key,如果有重复的就NG
		if checkRepeat(password) {
			fmt.Println("OK")
		} else {
			fmt.Println("NG")
		}
	}
}

// 校验重复子串
func checkRepeat(password string) bool {
	repeatMap := make(map[string]bool)
	var repeatKey string
	for i := 3; i < len(password); i++ {
		repeatKey = password[i-3 : i]
		if _, exist := repeatMap[repeatKey]; exist {
			return false
		} else {
			repeatMap[repeatKey] = true
		}
	}
	return true
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务