Strong Password Checker II — Solution in Python, Java & C++
Strong Password Checker II solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.
Strong Password Checker II is an easy coding problem on strings, asked in Amazon, Google and TCS interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Google, TCS
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A password is strong when all of the following hold:
- it is at least 8 characters long;
- it contains at least one lowercase letter;
- it contains at least one uppercase letter;
- it contains at least one digit;
- it contains at least one of
!@#$%^&*()-+; - it contains no two adjacent equal characters.
Return true if password is strong.
Example 1
Input: password = "CodeKairo#1"
Output: true
Explanation: Long enough, all four character classes, and no repeated neighbours.
Example 2
Input: password = "Coode#1Kairo"
Output: false
Explanation: The `oo` in `Coode` breaks the adjacency rule.
Example 3
Input: password = "Me+You--IsMyDream"
Output: false
Explanation: No digit, and `--` repeats.
Constraints
1 <= password.length <= 100password consists of letters, digits, and characters in "!@#$%^&*()-+".
Hints for Strong Password Checker II
Hint 1
Every rule can be checked in a single left-to-right pass.
Hint 2
Track four booleans for the character classes and compare each character with its predecessor.
Hint 3
Return false the moment two neighbours are equal — no later character can undo it.
How to solve Strong Password Checker II
One pass. Reject short passwords up front, then for each character update the four class flags and compare it with the one before it.
Approach
- If the length is under 8, return false.
- For each index, return false if the character equals its predecessor.
- Set the lowercase/uppercase/digit/special flag according to the character's class.
- Return the conjunction of the four flags.
Why it works
The adjacency rule is the only one that can fail early and is worth short-circuiting on; the four class flags can only ever turn on, so they are safe to accumulate and test at the end. Checking the classes with separate scans would be four passes for no gain.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Adjacent equality applies to any character, digits and specials included, not just letters.
- Exactly 8 characters is long enough — the bound is inclusive.
- The special set is fixed; characters outside it satisfy no class at all.
Reference solution
Python
def strongPasswordCheckerII(password: str) -> bool:
if len(password) < 8:
return False
special = "!@#$%^&*()-+"
lower = upper = digit = spec = False
for i, c in enumerate(password):
if i > 0 and c == password[i - 1]:
return False
if c.islower():
lower = True
elif c.isupper():
upper = True
elif c.isdigit():
digit = True
elif c in special:
spec = True
return lower and upper and digit and specJava
public static boolean strongPasswordCheckerII(String password) {
if (password.length() < 8) return false;
String special = "!@#$%^&*()-+";
boolean lower = false, upper = false, digit = false, spec = false;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (i > 0 && c == password.charAt(i - 1)) return false;
if (c >= 'a' && c <= 'z') lower = true;
else if (c >= 'A' && c <= 'Z') upper = true;
else if (c >= '0' && c <= '9') digit = true;
else if (special.indexOf(c) >= 0) spec = true;
}
return lower && upper && digit && spec;
}C++
bool strongPasswordCheckerII(string password) {
if (password.size() < 8) return false;
string special = "!@#$%^&*()-+";
bool lower = false, upper = false, digit = false, spec = false;
for (size_t i = 0; i < password.size(); i++) {
char c = password[i];
if (i > 0 && c == password[i - 1]) return false;
if (c >= 'a' && c <= 'z') lower = true;
else if (c >= 'A' && c <= 'Z') upper = true;
else if (c >= '0' && c <= '9') digit = true;
else if (special.find(c) != string::npos) spec = true;
}
return lower && upper && digit && spec;
}JavaScript
var strongPasswordCheckerII = function(password) {
if (password.length < 8) return false;
var SPECIAL = "!@#$%^&*()-+";
var lower = false, upper = false, digit = false, special = false;
for (var i = 0; i < password.length; i++) {
var c = password.charAt(i);
if (i > 0 && c === password.charAt(i - 1)) return false;
if (c >= "a" && c <= "z") lower = true;
else if (c >= "A" && c <= "Z") upper = true;
else if (c >= "0" && c <= "9") digit = true;
else if (SPECIAL.indexOf(c) !== -1) special = true;
}
return lower && upper && digit && special;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings