Skip to content

Commit b5f3176

Browse files
refactor 420
1 parent 689f309 commit b5f3176

File tree

1 file changed

+60
-53
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+60
-53
lines changed
Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,83 @@
11
package com.fishercoder.solutions;
22

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:
47
58
It has at least 6 characters and at most 20 characters.
69
It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.
710
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+
*/
1115
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];
2027

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+
}
3138

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;
3544
}
36-
arr[j] = i - j;
37-
}
3845

39-
int totalMissing = (a + A + d);
46+
int totalMissing = (a + A + d);
4047

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;
4754

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;
5262
}
53-
arr[i] -= Math.min(overLen, k);
54-
overLen -= k;
5563
}
56-
}
5764

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+
}
6471

65-
if (arr[i] >= 3) {
66-
leftOver += arr[i] / 3;
72+
if (arr[i] >= 3) {
73+
leftOver += arr[i] / 3;
74+
}
6775
}
76+
77+
res += Math.max(totalMissing, leftOver);
6878
}
6979

70-
res += Math.max(totalMissing, leftOver);
80+
return res;
7181
}
72-
73-
return res;
7482
}
75-
7683
}

0 commit comments

Comments
 (0)