Strong Password Checker - Problem

Imagine you're a security consultant tasked with strengthening weak passwords. Your job is to determine the minimum number of changes needed to transform any password into a fortress-level secure one!

A password achieves "strong" status when it satisfies ALL these criteria:

  • Length: Between 6-20 characters (inclusive)
  • Character diversity: At least one lowercase letter, one uppercase letter, and one digit
  • No repetition: No three consecutive identical characters (e.g., "aaa" is forbidden, but "aab" is fine)

You have three tools in your arsenal for each step:

  • Insert a character anywhere
  • Delete any character
  • Replace any character with another

Goal: Return the minimum steps needed to make the password strong. If it's already strong, return 0.

Input & Output

example_1.py โ€” Short Password
$ Input: password = "a"
โ€บ Output: 5
๐Ÿ’ก Note: The password needs 5 more characters to reach minimum length 6. We can add "A1bcd" to get "aA1bcd" which satisfies all requirements with 5 insertions.
example_2.py โ€” Normal Length with Issues
$ Input: password = "aA1"
โ€บ Output: 3
๐Ÿ’ก Note: Too short (need 3 more chars) but has all character types. Adding 3 characters like "bcd" gives us "aA1bcd" - minimum 3 steps.
example_3.py โ€” Long Password
$ Input: password = "1337C0d3"
โ€บ Output: 0
๐Ÿ’ก Note: This password is already strong: length 8 (within 6-20), has lowercase, uppercase, and digits, with no three consecutive repeating characters.

Visualization

Tap to expand
๐Ÿฐ Password Fortress Construction Strategy๐Ÿ—๏ธ Small Fortress (<6)Expansion StrategyAdd rooms & diverse defendersCost: max(types, 6-size)โš”๏ธ Knights ๐Ÿน Archers ๐Ÿ”ง Engineers๐Ÿ›๏ธ Medium Fortress (6-20)Reinforcement Strategy๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ โ†’ ๐Ÿ›ก๏ธโš”๏ธ๐Ÿ›ก๏ธ๐Ÿน๐Ÿน๐Ÿน โ†’ ๐Ÿน๐Ÿ”ง๐ŸนReplace weak spotsCost: max(missing_types, replacements)๐Ÿฏ Large Fortress (>20)Downsizing Strategy๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ โ†’ ๐Ÿ›ก๏ธโš”๏ธ๐Ÿ›ก๏ธStrategic demolition firstThen reinforce remainingCost: deletions + optimized_replacements๐ŸŽฏ Master Architect's WisdomExample: Fortress "aaaaaaaaaaaaaaaaaaaaaa" (22 guards, all identical)โŒ Novice approach: Remove 2 guards + Replace 7 identical groups = 9 operationsโœ… Master approach: Strategically remove 2 guards to break patterns optimallyThen replace remaining patterns โ†’ Only 8 operations total!๐Ÿ”‘ Key Insight: Each operation should solve multiple security issues simultaneouslyA true fortress builder thinks holistically - one strategic move can address size, diversity, and pattern weaknesses together!"The art of fortress building lies not in the number of changes, but in the wisdom of each change."
Understanding the Visualization
1
Assess Current State
Evaluate the fortress size, defender types, and structural weaknesses
2
Choose Strategy
Different approaches for small, medium, or oversized fortresses
3
Execute Plan
Make minimal changes that address multiple security concerns
4
Verify Security
Ensure all requirements are met with optimal resource usage
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy adapts to password length - expand small passwords with smart insertions, reinforce medium ones with targeted replacements, and downsize large ones with strategic deletions that minimize future work.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the password to analyze all constraints simultaneously

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only constant extra space for counters and state tracking

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค password.length โ‰ค 50
  • password consists of letters, digits, dot '.' or exclamation mark '!'.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
28.6K Views
Medium Frequency
~35 min Avg. Time
892 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