LeetCode 2299: Strong Password Checker II Solution
Master LeetCode problem 2299 (Strong Password Checker II), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2299. Strong Password Checker II
Problem Explanation
Explanation:
To solve this problem, we can iterate over the given password string while keeping track of whether it satisfies all the criteria for a strong password. We can maintain flags for the presence of lowercase letter, uppercase letter, digit, and special character. Additionally, we need to check for adjacent repeated characters. If any of these criteria are not met, we return false; otherwise, we return true at the end.
Algorithm:
- Initialize flags for lowercase, uppercase, digit, special character as false.
- Iterate over each character in the password string.
- Check each character against the criteria and update the corresponding flags.
- Also, check for adjacent repeated characters.
- If any criteria are not met, return false.
- Return true if all criteria are satisfied.
Time Complexity:
The time complexity of this approach is O(n), where n is the length of the password string.
Space Complexity:
The space complexity of this approach is O(1) as we are using a constant amount of extra space.
:
Solution Code
class Solution {
public boolean isStrongPassword(String password) {
boolean hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
for (char c : password.toCharArray()) {
if (Character.isLowerCase(c)) hasLower = true;
else if (Character.isUpperCase(c)) hasUpper = true;
else if (Character.isDigit(c)) hasDigit = true;
else if ("!@#$%^&*()-+".contains(String.valueOf(c))) hasSpecial = true;
}
for (int i = 0; i < password.length() - 1; i++) {
if (password.charAt(i) == password.charAt(i + 1)) return false;
}
return password.length() >= 8 && hasLower && hasUpper && hasDigit && hasSpecial;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2299 (Strong Password Checker II)?
This page provides optimized solutions for LeetCode problem 2299 (Strong Password Checker II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2299 (Strong Password Checker II)?
The time complexity for LeetCode 2299 (Strong Password Checker II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2299 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2299 in Java, C++, or Python.