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: "!@#$%^&*()-+".To solve #2299 Strong Password Checker II, the main idea is to validate a password string against a set of predefined security rules. Instead of performing multiple passes over the string, an efficient approach is to scan the password once and track whether each requirement has been satisfied.
During traversal, maintain boolean flags to check for lowercase letters, uppercase letters, digits, and special characters. A small set or lookup structure can be used to quickly determine whether a character belongs to the allowed special character list. At the same time, compare the current character with the previous one to ensure there are no identical adjacent characters.
The password is considered valid only if all conditions are satisfied and its length meets the minimum requirement. Since the string is processed in a single pass and only a few variables are used, this approach is both time‑efficient and memory‑efficient.
Time Complexity: O(n) where n is the length of the password.
Space Complexity: O(1) since only constant extra space is used.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Single-pass password validation with flags | O(n) | O(1) |
Colin Galen
Use these hints if you're stuck. Try solving on your own first.
You can use a boolean flag to define certain types of characters seen in the string.
In the end, check if all boolean flags have ended up True, and do not forget to check the "adjacent" and "length" criteria.
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, variations of password validation problems are common in coding interviews. They test a candidate's ability to work with strings, apply condition checks efficiently, and write clean validation logic.
A small hash set or lookup structure works well for storing the allowed special characters. It allows constant-time checks to determine whether a character is valid. Aside from that, only a few boolean variables are needed.
The optimal approach is to iterate through the password string once while tracking whether all required conditions are satisfied. Use boolean flags for lowercase, uppercase, digit, and special characters, and check that no adjacent characters are identical. This single-pass validation ensures efficiency.
The time complexity is O(n), where n is the length of the password string. The algorithm scans the string once and performs constant-time checks for each character.