forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2299.java
49 lines (47 loc) · 1.66 KB
/
_2299.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
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _2299 {
public static class Solution1 {
public boolean strongPasswordCheckerII(String password) {
if (password.length() < 8) {
return false;
}
boolean hasLower = false;
boolean hasUpper = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;
Set<Character> specialChars = new HashSet<>();
specialChars.add('!');
specialChars.add('@');
specialChars.add('%');
specialChars.add('^');
specialChars.add('&');
specialChars.add('*');
specialChars.add('(');
specialChars.add(')');
specialChars.add('-');
specialChars.add('+');
specialChars.add('$');
specialChars.add('#');
for (int i = 0; i < password.length(); i++) {
if (Character.isLowerCase(password.charAt(i))) {
hasLower = true;
}
if (Character.isUpperCase(password.charAt(i))) {
hasUpper = true;
}
if (Character.isDigit(password.charAt(i))) {
hasDigit = true;
}
if (specialChars.contains(password.charAt(i))) {
hasSpecialChar = true;
}
if (i > 0 && password.charAt(i) == password.charAt(i - 1)) {
return false;
}
}
return hasLower && hasUpper && hasDigit && hasSpecialChar;
}
}
}