题解 | 密码强度检查
密码强度检查
https://www.nowcoder.com/practice/40cc4cfe4a7d45839393fc305fc5609e
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
sc.nextLine();
String[] passwords = new String[T];
String[] strengths = new String[T];
REAL827 real827 = new REAL827();
for (int i = 0; i < T; i++) {
passwords[i] = sc.next();
strengths[i] = real827.passwordStrength(passwords[i]);
}
for (int i = 0; i < T; i++) {
System.out.println(strengths[i]);
}
}
}
class REAL827 {
public String passwordStrength(String password) {
int[] types = new int[password.length()];
if (password.length() < 8) {
return "Weak";
} else {
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= 33 && password.charAt(i) <= 126) {
if (password.charAt(i) >= 97 && password.charAt(i) <= 122) {
types[i] = 1;
} else if (password.charAt(i) >= 65 && password.charAt(i) <= 90) {
types[i] = 2;
} else if (password.charAt(i) >= 48 && password.charAt(i) <= 57) {
types[i] = 3;
} else {
types[i] = 4;
}
}
}
}
Set<Integer> typesSet = new HashSet<>();
for (int i = 0; i < password.length(); i++) {
typesSet.add(types[i]);
}
if (typesSet.contains(1) && typesSet.contains(2) && typesSet.contains(3) &&
typesSet.contains(4)) {
return "Strong";
} else if ((typesSet.contains(1) && typesSet.contains(2) &&
typesSet.contains(3)) ||
(typesSet.contains(2) && typesSet.contains(3) && typesSet.contains(4)) ||
(typesSet.contains(1) && typesSet.contains(3) && typesSet.contains(4)) ||
(typesSet.contains(1) && typesSet.contains(2) && typesSet.contains(4))) {
return "Medium";
} else {
return "Weak";
}
}
}
可能比较笨,但确实以目前水平最大限度在缩短时间复杂度
