A password is said to be strong if it satisfies all the following criteria:
8 characters."!@#$%^&*()-+".2 of the same character in adjacent positions (i.e., "aab" violates this condition, but "aba" does not).Given a string password, return true if it is a strong password. Otherwise, return false.
Example 1:
Input: password = "IloveLe3tcode!" Output: true Explanation: The password meets all the requirements. Therefore, we return true.
Example 2:
Input: password = "Me+You--IsMyDream" Output: false Explanation: 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 Explanation: The password does not meet the length requirement. Therefore, we return false.
Constraints:
1 <= password.length <= 100password consists of letters, digits, and special characters: "!@#$%^&*()-+".Problem Overview: You are given a password string and must determine if it qualifies as a strong password. A valid password must be at least 8 characters long, contain lowercase, uppercase, digit, and special characters, and must not have identical adjacent characters. The task is essentially a rule validation problem on a string.
Approach 1: Iterative Character Check (O(n) time, O(1) space)
Scan the password once while tracking whether each required category appears. During iteration, check if the current character is lowercase, uppercase, digit, or one of the allowed special characters. Maintain boolean flags for each requirement and immediately reject if two adjacent characters are equal. Because you only iterate through the string once and store a few flags, the solution runs in O(n) time with constant O(1) space.
This approach is straightforward and gives full control over each rule. It works well in interviews because you explicitly show how each condition is validated using simple character checks. The logic mostly relies on basic string traversal and conditional checks.
Approach 2: Regular Expression (O(n) time, O(1) space)
Another option is to validate the password using regular expressions. A regex pattern can enforce the presence of lowercase letters, uppercase letters, digits, and special characters using lookaheads. After the regex match, perform an additional scan to ensure no adjacent characters are identical.
The regex engine still processes the string linearly, so the complexity remains O(n) time with constant O(1) extra space. This method produces shorter code and is convenient when multiple character rules must be validated simultaneously. However, debugging complex regex patterns can be harder during interviews.
Recommended for interviews: The iterative character check approach is usually preferred. It clearly demonstrates how you validate constraints, detect adjacent duplicates, and manage state while scanning a string. Regex solutions are concise but may hide the underlying logic, while the iterative solution shows stronger problem-solving clarity.
This approach involves iterating through each character of the password while maintaining flags that check for different conditions like uppercase, lowercase, digit, special character, and adjacent duplicates. Start by checking if the length of the password is at least 8. Update flags as you iterate through the password and check if two consecutive characters are the same. If all flags are true and no two consecutive characters are the same, then the password is strong.
The C implementation uses the standard library to check character types and maintain flags to ensure the password meets all conditions without adjacent duplicates.
Time Complexity: O(n), Space Complexity: O(1) - iterating through the password once with a fixed number of flags.
This method simplifies many checks by leveraging a regular expression to validate parts of the password rules. The regex checks can determine the presence of character types and length while additional logic ensures no adjacent duplicates are present. This approach makes the code cleaner but requires a good understanding of regex.
C code employs regular expressions for matching multiple criteria in one line, although C's regex library involves additional complexity with compiling and executing expressions.
Time Complexity: O(n), Space Complexity: O(1).
According to the problem description, we can simulate the process of checking whether the password meets the requirements.
First, we check if the length of the password is less than 8. If it is, we return false.
Next, we use a mask mask to record whether the password contains lowercase letters, uppercase letters, digits, and special characters. We traverse the password, and for each character, we first check if it is the same as the previous character. If it is, we return false. Then, we update the mask mask based on the character type. Finally, we check if the mask mask is 15. If it is, we return true; otherwise, we return false.
The time complexity is O(n), and the space complexity is O(1). Here, n is the length of the password.
| Approach | Complexity |
|---|---|
| Approach 1: Iterative Character Check | Time Complexity: O(n), Space Complexity: O(1) - iterating through the password once with a fixed number of flags. |
| Approach 2: Regular Expression | Time Complexity: O(n), Space Complexity: O(1). |
| Simulation + Bit Manipulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Character Check | O(n) | O(1) | Best general solution. Clear rule validation and preferred in coding interviews. |
| Regular Expression | O(n) | O(1) | Useful when combining multiple character checks compactly using regex patterns. |
LeetCode 2299 – How to Check Strong Passwords in Java (Easy Explanation) • Recurso • 558 views views
Watch 7 more video solutions →Practice Strong Password Checker II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor