Strong Password Checker II - Problem

A password is said to be strong if it satisfies all the following criteria:

  • It has at least 8 characters.
  • It contains at least one lowercase letter.
  • It contains at least one uppercase letter.
  • It contains at least one digit.
  • It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+".
  • It does not contain 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.

Input & Output

Example 1 — Strong Password
$ Input: password = "ILoveLeetcode"
Output: false
💡 Note: Password has 13 characters with uppercase, lowercase letters but lacks digits and special characters. Also has adjacent 'e' characters.
Example 2 — Valid Strong Password
$ Input: password = "Abc1234!"
Output: true
💡 Note: Password meets all criteria: 8+ chars, has uppercase (A), lowercase (b,c), digit (1,2,3,4), special (!), no adjacent duplicates.
Example 3 — Adjacent Duplicates
$ Input: password = "Password123!!"
Output: false
💡 Note: Password has adjacent duplicate characters '!!' which violates the no adjacent duplicates rule.

Constraints

  • 1 ≤ password.length ≤ 100
  • password consists of letters, digits, and special characters: "!@#$%^&*()-+".

Visualization

Tap to expand
Strong Password Checker II INPUT Password String: I L o v e L e e t c o d e "ILoveLeetcode" Length: 13 chars Requirements: 1. Min 8 characters 2. Has lowercase 3. Has uppercase 4. Has digit 5. Has special char 6. No adjacent duplicates Special: !@#$%^&*()-+ ALGORITHM STEPS 1 Check Length 13 >= 8 --> OK 2 Check Lowercase 'o','v','e'... found --> OK 3 Check Uppercase 'I','L' found --> OK 4 Check Digit No digit found --> FAIL 5 Check Special No special char --> FAIL 6 Adjacent Check No duplicates --> OK Validation Summary: 4/6 checks passed Missing: digit, special char FINAL RESULT false NOT Strong Password Missing Requirements: No digit (0-9) No special char Passed Checks: OK Length, Lower, Upper OK No adjacent dups Output: false Key Insight: Single Pass Validation iterates through the password once, checking all 6 criteria simultaneously. Use boolean flags for each requirement. If ANY flag remains false after scanning, return false. Time: O(n) where n = password length | Space: O(1) constant for flags TutorialsPoint - Strong Password Checker II | Single Pass Validation
Asked in
Microsoft 25 Facebook 20
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen