-
Notifications
You must be signed in to change notification settings - Fork 16
/
StrongPasswordCheckerII.java
77 lines (67 loc) · 2.33 KB
/
StrongPasswordCheckerII.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// https://leetcode.com/problems/strong-password-checker-ii
// T: O(|password|)
// S: O(1)
import java.util.HashSet;
import java.util.Set;
public class StrongPasswordCheckerII {
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-+";
public boolean strongPasswordCheckerII(String password) {
return atLeastLen8(password)
&& atLeast1LowercaseLetter(password)
&& atLeast1UppercaseLetter(password)
&& atLeast1Digit(password)
&& atLeast1SpecialCharacter(password)
&& noAdjacentSameCharacters(password);
}
private boolean noAdjacentSameCharacters(String password) {
for (int i = 1 ; i < password.length() ; i++) {
if (password.charAt(i - 1) == password.charAt(i)) {
return false;
}
}
return true;
}
private boolean atLeast1SpecialCharacter(String password) {
final Set<Character> characters = charactersOf(password);
for (int i = 0 ; i < SPECIAL_CHARACTERS.length() ; i++) {
if (characters.contains(SPECIAL_CHARACTERS.charAt(i))) {
return true;
}
}
return false;
}
private Set<Character> charactersOf(String password) {
final Set<Character> set = new HashSet<>();
for (int i = 0 ; i < password.length() ; i++) {
set.add(password.charAt(i));
}
return set;
}
private boolean atLeast1Digit(String password) {
for (int i = 0 ; i < password.length() ; i++) {
if (password.charAt(i) >= '0' && password.charAt(i) <= '9') {
return true;
}
}
return false;
}
private boolean atLeast1UppercaseLetter(String password) {
for (int i = 0 ; i < password.length() ; i++) {
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z') {
return true;
}
}
return false;
}
private boolean atLeast1LowercaseLetter(String password) {
for (int i = 0 ; i < password.length() ; i++) {
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') {
return true;
}
}
return false;
}
private boolean atLeastLen8(String password) {
return password.length() >= 8;
}
}