Skip to content

Commit 4c93e45

Browse files
solves #2299: Strong Password Checker II in java
1 parent b71e7e4 commit 4c93e45

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@
740740
| 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value) | [![Java](assets/java.png)](src/CheckIfNumberHasEqualDigitCountAndDigitValue.java) | |
741741
| 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string) | [![Java](assets/java.png)](src/RearrangeCharactersToMakeTargetString.java) | |
742742
| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game) | [![Java](assets/java.png)](src/MinMaxGame.java) | |
743-
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii) | | |
743+
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii) | [![Java](assets/java.png)](src/StrongPasswordCheckerII.java) | |
744744
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
745745
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
746746
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |

Diff for: src/StrongPasswordCheckerII.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// https://leetcode.com/problems/strong-password-checker-ii
2+
// T: O(|password|)
3+
// S: O(1)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class StrongPasswordCheckerII {
9+
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-+";
10+
11+
public boolean strongPasswordCheckerII(String password) {
12+
return atLeastLen8(password)
13+
&& atLeast1LowercaseLetter(password)
14+
&& atLeast1UppercaseLetter(password)
15+
&& atLeast1Digit(password)
16+
&& atLeast1SpecialCharacter(password)
17+
&& noAdjacentSameCharacters(password);
18+
}
19+
20+
private boolean noAdjacentSameCharacters(String password) {
21+
for (int i = 1 ; i < password.length() ; i++) {
22+
if (password.charAt(i - 1) == password.charAt(i)) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
private boolean atLeast1SpecialCharacter(String password) {
30+
final Set<Character> characters = charactersOf(password);
31+
for (int i = 0 ; i < SPECIAL_CHARACTERS.length() ; i++) {
32+
if (characters.contains(SPECIAL_CHARACTERS.charAt(i))) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
private Set<Character> charactersOf(String password) {
40+
final Set<Character> set = new HashSet<>();
41+
for (int i = 0 ; i < password.length() ; i++) {
42+
set.add(password.charAt(i));
43+
}
44+
return set;
45+
}
46+
47+
private boolean atLeast1Digit(String password) {
48+
for (int i = 0 ; i < password.length() ; i++) {
49+
if (password.charAt(i) >= '0' && password.charAt(i) <= '9') {
50+
return true;
51+
}
52+
}
53+
return false;
54+
}
55+
56+
private boolean atLeast1UppercaseLetter(String password) {
57+
for (int i = 0 ; i < password.length() ; i++) {
58+
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z') {
59+
return true;
60+
}
61+
}
62+
return false;
63+
}
64+
65+
private boolean atLeast1LowercaseLetter(String password) {
66+
for (int i = 0 ; i < password.length() ; i++) {
67+
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') {
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
73+
74+
private boolean atLeastLen8(String password) {
75+
return password.length() >= 8;
76+
}
77+
}

0 commit comments

Comments
 (0)