Skip to content

Commit d00a30c

Browse files
committed
2022-06-18 update: added "Strong Password Checker II"
1 parent ea6adff commit d00a30c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
// https://leetcode.com/problems/strong-password-checker-ii/
7+
public class StrongPasswordCheckerII {
8+
9+
private final String input;
10+
11+
public StrongPasswordCheckerII(String input) {
12+
this.input = input;
13+
}
14+
15+
public boolean solution() {
16+
if (input.length() < 8) {
17+
return false;
18+
}
19+
Set<Character> special = new HashSet<>();
20+
String specialChars = "!@#$%^&*()-+";
21+
for (int i = 0; i < specialChars.length(); i++) {
22+
special.add(specialChars.charAt(i));
23+
}
24+
char prev = input.charAt(0);
25+
int digits = Character.isDigit(prev) ? 1 : 0;
26+
int upper = Character.isUpperCase(prev) ? 1 : 0;
27+
int lower = Character.isLowerCase(prev) ? 1 : 0;
28+
int spec = special.contains(prev) ? 1 : 0;
29+
for (int i = 1; i < input.length(); i++) {
30+
char curr = input.charAt(i);
31+
if (curr == prev) {
32+
return false;
33+
}
34+
digits += Character.isDigit(curr) ? 1 : 0;
35+
upper += Character.isUpperCase(curr) ? 1 : 0;
36+
lower += Character.isLowerCase(curr) ? 1 : 0;
37+
spec += special.contains(curr) ? 1 : 0;
38+
prev = curr;
39+
}
40+
return digits > 0 && upper > 0 && lower > 0 && spec > 0;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class StrongPasswordCheckerIITest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertTrue(new StrongPasswordCheckerII("IloveLe3tcode!").solution());
12+
}
13+
14+
}

0 commit comments

Comments
 (0)