题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func main() {
reader := bufio.NewScanner(os.Stdin)
for reader.Scan(){
s := reader.Text()
if valid(s){
fmt.Println("OK")
}else{
fmt.Println("NG")
}
}
}
func valid(s string)bool{
if len(s)<=8{
return false
}
hasUper,hasLower,hasDigit,hasSpecial := false,false,false,false
for _,ch := range s{
if unicode.IsUpper(ch){
hasUper = true
}else if unicode.IsLower(ch){
hasLower = true
}else if unicode.IsDigit(ch){
hasDigit = true
}else{
hasSpecial = true
}
}
count :=0
if hasUper{
count++
}
if hasLower{
count++
}
if hasDigit{
count++
}
if hasSpecial{
count++
}
if count<3{
return false
}
// 使用map来确定是否出现过
m := make(map[string]int)
for length:=3;length<=len(s);length++{
for i:=0;i<len(s)-length;i++{
subStr := s[i:i+length]
if startIndex,ok:=m[subStr];ok{
if i>=startIndex+length{ // 不是相同的字符串,并且字段不重合
return false
}
}else{
m[subStr] = i
}
}
}
return true
}
查看10道真题和解析