Skip to content

Commit 0da24b3

Browse files
authored
Create 2299-strong-password-checker-ii.java
1 parent 11d7c43 commit 0da24b3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* LeetCode Problem: 2299. Strong Password Checker II
3+
* Link: https://leetcode.com/problems/strong-password-checker-ii/
4+
*
5+
* Description:
6+
* A password is said to be strong if:
7+
* - It has at least 8 characters.
8+
* - It contains at least one lowercase letter.
9+
* - It contains at least one uppercase letter.
10+
* - It contains at least one digit.
11+
* - It contains at least one special character from the string "!@#$%^&*()-+".
12+
* - It does not contain two identical characters in adjacent positions.
13+
*
14+
* Given a string password, return true if it is a strong password. Otherwise, return false.
15+
*
16+
* ------------------------------------------------------------------------
17+
* Time Complexity: O(n) // iterate through the string once
18+
* Space Complexity: O(1) // only a few boolean flags used
19+
* ------------------------------------------------------------------------
20+
*/
21+
22+
class Solution {
23+
public boolean strongPasswordCheckerII(String password) {
24+
if (password.length() < 8) return false;
25+
26+
boolean lower = false, upper = false, digit = false, special = false;
27+
String specials = "!@#$%^&*()-+";
28+
29+
for (int i = 0; i < password.length(); i++) {
30+
char c = password.charAt(i);
31+
32+
if (Character.isLowerCase(c)) lower = true;
33+
else if (Character.isUpperCase(c)) upper = true;
34+
else if (Character.isDigit(c)) digit = true;
35+
else if (specials.indexOf(c) >= 0) special = true;
36+
37+
// check adjacent characters
38+
if (i > 0 && c == password.charAt(i - 1)) return false;
39+
}
40+
41+
return lower && upper && digit && special;
42+
}
43+
}

0 commit comments

Comments
 (0)