题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; public class Main { static final String OK = "OK"; static final String NG = "NG"; public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str = in.nextLine(); if (isLegalLength(str) && hasEnoughComplexity(str) && hasNoIllegalSubString(str)) { System.out.println(OK); } else { System.out.println(NG); } } } static boolean isLegalLength(String password) { return password.length() > 8; } static boolean containsRange(int c, int s, int e) { return s <= c && c <= e; } static boolean hasEnoughComplexity(String password) { int complexity = 0; for (int i = 0; i < password.length(); i++) { if (Integer.bitCount(complexity) > 2) { return true; } char c = password.charAt(i); boolean hasDigit = containsRange(c, '0', '9'); boolean hasUpperLetter = containsRange(c, 'A', 'Z'); boolean hasLowerLetter = containsRange(c, 'a', 'z'); boolean hasOthers = !(hasDigit || hasUpperLetter || hasLowerLetter); if (hasDigit) { complexity |= 1; } if (hasUpperLetter) { complexity |= (1 << 2); } if (hasLowerLetter) { complexity |= (1 << 3); } if (hasOthers) { complexity |= (1 << 4); } } return false; } static boolean hasNoIllegalSubString(String password) { for (int i = 3; i < password.length(); i++) { if (password.substring(i).contains(password.substring(i - 3, i))) { return false; } } return true; } }