LeetCode Problem Workspace
Strong Password Checker II
Check if a given password meets all strength requirements, including length, character types, and no adjacent repeated characters.
1
Topics
7
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Check if a given password meets all strength requirements, including length, character types, and no adjacent repeated characters.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
To solve the Strong Password Checker II problem, check if the password meets length and character type requirements. Additionally, verify that no two adjacent characters are identical. A boolean flag can be used to track character types and detect adjacency violations efficiently.
Problem Statement
A password is considered strong if it meets the following conditions: it is at least 8 characters long, contains at least one lowercase letter, one uppercase letter, one digit, and one special character. Additionally, the password must not have two identical characters in adjacent positions.
Given a string password, your task is to return true if it satisfies all the above conditions. Otherwise, return false. The password consists of letters, digits, and special characters: '!@#$%^&*()-+'. The length of the password will be between 1 and 100 characters.
Examples
Example 1
Input: password = "IloveLe3tcode!"
Output: true
The password meets all the requirements. Therefore, we return true.
Example 2
Input: password = "Me+You--IsMyDream"
Output: false
The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.
Example 3
Input: password = "1aB!"
Output: false
The password does not meet the length requirement. Therefore, we return false.
Constraints
- 1 <= password.length <= 100
- password consists of letters, digits, and special characters: "!@#$%^&*()-+".
Solution Approach
Check Length and Character Types
The first step is to check if the password length is at least 8 characters. Then, iterate through the string to verify the presence of at least one lowercase letter, one uppercase letter, one digit, and one special character. A boolean flag can help track the presence of each character type.
Check for Adjacent Repeated Characters
The next step is to check for adjacent repeated characters. You can compare each character with its next character as you iterate through the password string. If two adjacent characters are identical, return false.
Return True or False
After checking both conditions, if the password passes all the checks, return true. If any condition fails, return false. Ensure that the implementation has efficient checks to handle the maximum string length.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time complexity of this solution is O(n), where n is the length of the password, because we only need to scan the string once to check all conditions. The space complexity is O(1) since we are only using a few boolean flags to track character types and adjacent characters.
What Interviewers Usually Probe
- Look for a solution that efficiently checks each condition while maintaining clarity.
- Candidates should avoid unnecessary loops and optimize string traversal.
- Watch for candidates who struggle with the adjacency check, as this could indicate a misunderstanding of the requirements.
Common Pitfalls or Variants
Common pitfalls
- Failing to handle all the character types correctly, especially when characters like special symbols are overlooked.
- Not checking for adjacent identical characters, which is a critical part of the problem.
- Overcomplicating the solution with multiple nested loops or excessive space complexity.
Follow-up variants
- Modify the problem to require a minimum of two special characters.
- Change the password rules to allow only certain special characters (e.g., only '@' or '!').
- Allow passwords of length 6 or more, testing if the solution scales with different constraints.
FAQ
How do I check if a password meets all strength requirements?
You need to check if the password meets length and character type requirements while also ensuring there are no adjacent identical characters.
What happens if the password doesn't meet the length requirement?
If the password is shorter than 8 characters, it will immediately fail the check and return false.
What if a password contains special characters but no digits?
The password will fail because it must contain at least one digit, one lowercase letter, one uppercase letter, and one special character.
What does 'adjacent characters' mean in this problem?
Adjacent characters refer to two consecutive characters in the password that are identical. If such a pair is found, the password fails the check.
Can I use a boolean flag to simplify the solution?
Yes, a boolean flag is a great way to track the presence of character types and to simplify the solution, especially when detecting adjacent identical characters.
Solution
Solution 1: Simulation + Bit Manipulation
According to the problem description, we can simulate the process of checking whether the password meets the requirements.
class Solution:
def strongPasswordCheckerII(self, password: str) -> bool:
if len(password) < 8:
return False
mask = 0
for i, c in enumerate(password):
if i and c == password[i - 1]:
return False
if c.islower():
mask |= 1
elif c.isupper():
mask |= 2
elif c.isdigit():
mask |= 4
else:
mask |= 8
return mask == 15Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward