1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| 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 >= 'A' && c <= 'Z') B = 1; else if (c >= '0' && c <= '9') C = 1; } int m = A + B + C; if (n < 6) { return Math.max(6 - n, 3 - m); } else if (n <= 20) { int total = 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) total += cnt / 3; i = j; } return Math.max(total, 3 - m); } else { int total = 0; int[] cnts = 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) { total += cnt / 3; ++cnts[cnt % 3]; } i = j; } int base = n - 20, cur = base; for (int i = 0; i < 3; ++i) { if (i == 2) cnts[i] = total; if (cnts[i] != 0 && cur != 0) { int t = Math.min(cnts[i] * (i + 1), cur); cur -= t; total -= t / (i + 1); } } return base + Math.max(total, 3 - m); } } }
|