java:
力扣谢谢三叶姐 真的无聊题 直接看了一遍cv了 分类讨论 容易出错 但是感觉不太考的模拟题
class Solution {
public int strongPasswordChecker(String password) {
char[] cs = password.toCharArray();
int n = cs.length;
int A = 0, B = 0, C = 0;
for (char c : cs) {
if (c >= 'a' && c <= 'z') A = 1;
else if (c >= '0' && c <= '9') B = 1;
else C = 1;
}
int m = A + B + C;
if (n < 6) {
return Math.max(6 - n, 3 - m);
} else if (n <= 20) {
int tot = 0;
for (int i = 0; i < n; ) {
int j = i;
while (j < n && cs[j] == cs[i]) j++;
int cnt = j - i;
if (cnt >= 3) tot += cnt / 3;
i = j;
}
return Math.max(tot, 3 - m);
} else {
int tot = 0;
int[] remain = new int[3];
for (int i = 0; i < n; ) {
int j = i;
while (j < n && cs[j] == cs[i]) j++;
int cnt = j - i;
if (cnt >= 3) {
tot += cnt / 3; remain[cnt % 3]++;
}
i = j;
}
int base = n - 20, cur = base;
for (int i = 0; i < 3; i++) {
if (i == 2) remain[i] = tot;
if (remain[i] != 0 && cur > 0) {
int t = Math.min(remain[i] * (i + 1), cur);
cur -= t; tot -= t / (i + 1);
}
}
return base + Math.max(tot, 3 - m);
}
}
}