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
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
โ Linear Growth
Space Complexity
O(1)
Only constant extra space for counters and state tracking
โ Linear Space
Constraints
- 1 โค password.length โค 50
-
password consists of letters, digits, dot
'.'or exclamation mark'!'.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code