题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { String str = null; while ((str = br.readLine()) != null) { if (str.length() <= 8) { System.out.println("NG"); return; } List<Character> lowercases = new ArrayList<>(); for (int i = 0; i < 26; i++) { lowercases.add((char)(97 + i)); } List<Character> uppercases = new ArrayList<>(); for (int i = 0; i < 26; i++) { uppercases.add((char)(65 + i)); } List<Integer> numbers = new ArrayList<>(); for (int i = 0; i < 10; i++) { numbers.add(i); } List<Character> characters = new ArrayList<>(); char[] specials = "~!@#$%^&*_,./\\\\?|".toCharArray(); for (int i = 0; i < specials.length; i++) { characters.add(specials[i]); } char[] chars = str.toCharArray(); int lowercaseCount = 0; int uppercaseCount = 0; int numberCount = 0; int specialCount = 0; for (int i = 0; i < chars.length; i++) { if (lowercases.contains(chars[i])) { lowercaseCount++; } else if (uppercases.contains(chars[i])) { uppercaseCount++; } else if (characters.contains(chars[i])) { specialCount++; } else if (numbers.contains(chars[i] - '0')) { numberCount++; } } int count = 0; count = lowercaseCount > 0 ? ++count : count; count = uppercaseCount > 0 ? ++count : count; count = numberCount > 0 ? ++count : count; count = specialCount > 0 ? ++count : count; if (count >= 3) { List<String> list = new ArrayList<>(); boolean b = true; for (int j = 0; j < str.length(); j++) { int endIndex = j + 3 >= str.length() ? str.length() : j + 3; String substring = str.substring(j, endIndex); if (!list.contains(substring)) { list.add(substring); } else { b = false; break; } } if (b) { System.out.println("OK"); } else { System.out.println("NG"); } } else { System.out.println("NG"); } } } catch (Exception e) { e.printStackTrace(); } } }