题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String str = sc.nextLine(); if (isOk(str)) { System.out.println("OK"); } else { System.out.println("NG"); } } } //判断密码是否符合要求 public static boolean isOk(String s) { //长度超过8位 if (s.length() <= 8) { return false; } //字符串应包括大小写字母、数字、其它符号,以上四种至少三种 int n1 = 0, n2 = 0, n3 = 0, n4 = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') { n1 = 1; } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') { n2 = 1; } else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') { n3 = 1; } else { n4 = 1; } } if (n1 + n2 + n3 + n4 < 3) { return false; } //不能有长度为2的包含公共元素的子串重复 for (int j = 0; j < s.length() - 3; j++) { String str = s.substring(j, j + 3); if (s.lastIndexOf(str) != j) { return false; } } return true; } }