|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**A password is considered strong if below conditions are all met: |
| 3 | +/** |
| 4 | + * 420. Strong Password Checker |
| 5 | + * |
| 6 | + * A password is considered strong if below conditions are all met: |
4 | 7 |
|
5 | 8 | It has at least 6 characters and at most 20 characters.
|
6 | 9 | It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.
|
7 | 10 | It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).
|
8 |
| - Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0. |
9 |
| -
|
10 |
| - Insertion, deletion or replace of any one character are all considered as one change.*/ |
| 11 | + Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM |
| 12 | + change required to make s a strong password. If s is already strong, return 0. |
| 13 | + Insertion, deletion or replace of any one character are all considered as one change. |
| 14 | + */ |
11 | 15 | public class _420 {
|
12 |
| - /**Looked at this solution: https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-condition*/ |
13 |
| - public int strongPasswordChecker(String s) { |
14 |
| - int res = 0; |
15 |
| - int a = 1; |
16 |
| - int A = 1; |
17 |
| - int d = 1; |
18 |
| - char[] carr = s.toCharArray(); |
19 |
| - int[] arr = new int[carr.length]; |
| 16 | + public static class Solution1 { |
| 17 | + /** |
| 18 | + * credit: https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-condition |
| 19 | + */ |
| 20 | + public int strongPasswordChecker(String s) { |
| 21 | + int res = 0; |
| 22 | + int a = 1; |
| 23 | + int A = 1; |
| 24 | + int d = 1; |
| 25 | + char[] carr = s.toCharArray(); |
| 26 | + int[] arr = new int[carr.length]; |
20 | 27 |
|
21 |
| - for (int i = 0; i < arr.length;) { |
22 |
| - if (Character.isLowerCase(carr[i])) { |
23 |
| - a = 0; |
24 |
| - } |
25 |
| - if (Character.isUpperCase(carr[i])) { |
26 |
| - A = 0; |
27 |
| - } |
28 |
| - if (Character.isDigit(carr[i])) { |
29 |
| - d = 0; |
30 |
| - } |
| 28 | + for (int i = 0; i < arr.length; ) { |
| 29 | + if (Character.isLowerCase(carr[i])) { |
| 30 | + a = 0; |
| 31 | + } |
| 32 | + if (Character.isUpperCase(carr[i])) { |
| 33 | + A = 0; |
| 34 | + } |
| 35 | + if (Character.isDigit(carr[i])) { |
| 36 | + d = 0; |
| 37 | + } |
31 | 38 |
|
32 |
| - int j = i; |
33 |
| - while (i < carr.length && carr[i] == carr[j]) { |
34 |
| - i++; |
| 39 | + int j = i; |
| 40 | + while (i < carr.length && carr[i] == carr[j]) { |
| 41 | + i++; |
| 42 | + } |
| 43 | + arr[j] = i - j; |
35 | 44 | }
|
36 |
| - arr[j] = i - j; |
37 |
| - } |
38 | 45 |
|
39 |
| - int totalMissing = (a + A + d); |
| 46 | + int totalMissing = (a + A + d); |
40 | 47 |
|
41 |
| - if (arr.length < 6) { |
42 |
| - res += totalMissing + Math.max(0, 6 - (arr.length + totalMissing)); |
43 |
| - } else { |
44 |
| - int overLen = Math.max(arr.length - 20, 0); |
45 |
| - int leftOver = 0; |
46 |
| - res += overLen; |
| 48 | + if (arr.length < 6) { |
| 49 | + res += totalMissing + Math.max(0, 6 - (arr.length + totalMissing)); |
| 50 | + } else { |
| 51 | + int overLen = Math.max(arr.length - 20, 0); |
| 52 | + int leftOver = 0; |
| 53 | + res += overLen; |
47 | 54 |
|
48 |
| - for (int k = 1; k < 3; k++) { |
49 |
| - for (int i = 0; i < arr.length && overLen > 0; i++) { |
50 |
| - if (arr[i] < 3 || arr[i] % 3 != (k - 1)) { |
51 |
| - continue; |
| 55 | + for (int k = 1; k < 3; k++) { |
| 56 | + for (int i = 0; i < arr.length && overLen > 0; i++) { |
| 57 | + if (arr[i] < 3 || arr[i] % 3 != (k - 1)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + arr[i] -= Math.min(overLen, k); |
| 61 | + overLen -= k; |
52 | 62 | }
|
53 |
| - arr[i] -= Math.min(overLen, k); |
54 |
| - overLen -= k; |
55 | 63 | }
|
56 |
| - } |
57 | 64 |
|
58 |
| - for (int i = 0; i < arr.length; i++) { |
59 |
| - if (arr[i] >= 3 && overLen > 0) { |
60 |
| - int need = arr[i] - 2; |
61 |
| - arr[i] -= overLen; |
62 |
| - overLen -= need; |
63 |
| - } |
| 65 | + for (int i = 0; i < arr.length; i++) { |
| 66 | + if (arr[i] >= 3 && overLen > 0) { |
| 67 | + int need = arr[i] - 2; |
| 68 | + arr[i] -= overLen; |
| 69 | + overLen -= need; |
| 70 | + } |
64 | 71 |
|
65 |
| - if (arr[i] >= 3) { |
66 |
| - leftOver += arr[i] / 3; |
| 72 | + if (arr[i] >= 3) { |
| 73 | + leftOver += arr[i] / 3; |
| 74 | + } |
67 | 75 | }
|
| 76 | + |
| 77 | + res += Math.max(totalMissing, leftOver); |
68 | 78 | }
|
69 | 79 |
|
70 |
| - res += Math.max(totalMissing, leftOver); |
| 80 | + return res; |
71 | 81 | }
|
72 |
| - |
73 |
| - return res; |
74 | 82 | }
|
75 |
| - |
76 | 83 | }
|
0 commit comments