Watch 8 video solutions for Strong Password Checker II, a easy level problem involving String. This walkthrough by Recurso has 558 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |