Strong Password Checker - Problem

A password is considered strong if all of the following conditions are met:

  • It has at least 6 characters and at most 20 characters.
  • It contains at least one lowercase letter, at least one uppercase letter, and at least one digit.
  • It does not contain three repeating characters in a row (i.e., "Baaabb0" is weak, but "Baaba0" is strong).

Given a string password, return the minimum number of steps required to make the password strong. If the password is already strong, return 0.

In one step, you can:

  • Insert one character to password
  • Delete one character from password
  • Replace one character of password with another character

Input & Output

Example 1 — Short Password
$ Input: password = "a"
Output: 5
💡 Note: Password is too short (1 < 6) and missing uppercase and digit. Need max(6-1, 3) = 5 insertions: "a" → "Aa1bcd"
Example 2 — Repeating Characters
$ Input: password = "aaa111"
Output: 2
💡 Note: Length is 6 (good), has lowercase and digits, missing uppercase. Has "aaa" and "111" repeating. Need max(1 missing type, 2 replacements) = 2 steps.
Example 3 — Already Strong
$ Input: password = "Aa1bcde"
Output: 0
💡 Note: Length 7 is valid, has all character types, no repeating sequences. Already strong.

Constraints

  • 1 ≤ password.length ≤ 50
  • password consists of letters, digits, dot '.' or exclamation mark '!'

Visualization

Tap to expand
Strong Password Checker: "aaa" → Strong PasswordInput"aaa"Length: 3 (too short)Missing: Upper, DigitRepeats: "aaa"AnalysisShort: Use Insertions StrategyNeed: max(6-3, 3) = 3 insertionsCan fix multiple issues per stepOutput5 steps"aaa" → "Aa1abc"Strong password achieved🎯 Key Insight: Different lengths need different strategies for optimal results
Understanding the Visualization
1
Input Analysis
Check length, character types, and repeating sequences
2
Strategy Selection
Choose optimal operation type based on length
3
Minimize Operations
Combine fixes to reduce total steps
Key Takeaway
🎯 Key Insight: Different password lengths require different optimization strategies to minimize total operations
Asked in
Google 15 Facebook 12 Microsoft 8
89.2K Views
Medium Frequency
~35 min Avg. Time
1.8K 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