Strong Password Checker - Solution & Explanation
Problem Statement
A password is considered strong if the below conditions are all met:
- It has at least
6characters and at most20characters. - 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 password strong. if password is already strong, return 0.
In one step, you can:
- Insert one character to
password, - Delete one character from
password, or - Replace one character of
passwordwith another character.
Example 1:
Input: password = "a" Output: 5
Example 2:
Input: password = "aA1" Output: 3
Example 3:
Input: password = "1337C0d3" Output: 0
Constraints:
1 <= password.length <= 50passwordconsists of letters, digits, dot'.'or exclamation mark'!'.
Approach Overview
Problem Overview: You are given a password string and must return the minimum number of changes required to make it a strong password. A strong password must have length between 6 and 20, contain at least one lowercase letter, one uppercase letter, and one digit, and must not contain three repeating characters in a row.
Approach 1: Greedy Approach with Replacements (O(n) time, O(1) space)
This method scans the string once while tracking three things: missing character types (lowercase, uppercase, digit), repeating sequences, and the current length of the password. For repeating runs like aaa or bbbb, you count how many replacements are required using length / 3. When the password is shorter than 6, insertions fix both length and missing character types. When it exceeds 20 characters, deletions are prioritized inside long repeating sequences because removing characters reduces the number of replacements required. The greedy insight is that deletions should target sequences where they reduce replacement cost the most. This strategy efficiently balances insert, delete, and replace operations using rules derived from repetition lengths. The algorithm processes the string in O(n) time and constant extra space, making it the standard optimal solution for this string and greedy problem.
Approach 2: Iterative Character Update (O(n) time, O(1) space)
This approach also walks through the password but directly simulates corrections step by step. You track missing character classes and detect repeating groups during iteration. Instead of calculating all operations mathematically upfront, the algorithm progressively updates counts for insertions, deletions, and replacements as it encounters violations. For long passwords, extra characters are removed while prioritizing sections with repeating patterns. For shorter ones, insertions are used to both extend the password and break repetition sequences. The method is straightforward to implement and works well in languages like C++ and JavaScript where iterative mutation is convenient.
Heap Optimization (conceptual extension): Some implementations push repeating segment lengths into a heap (priority queue). During deletion phases for passwords longer than 20, segments that benefit most from deletion are processed first. While not required for the optimal solution, this structure helps visualize the greedy prioritization.
Recommended for interviews: The greedy replacement strategy is what most interviewers expect. It demonstrates that you understand how to combine constraints—length limits, character diversity, and repetition rules—into a single linear pass. Brute reasoning about each constraint separately is useful during discussion, but the greedy solution shows the ability to optimize operations and reach the minimal number of edits.
Approach 1: Greedy Approach with Replacements
This approach works by identifying deficiencies in the password (length, missing character types, consecutive characters) and addressing them by replacements wherever necessary to minimize the number of operations required.
The Python solution uses a greedy approach to check the password's strength. It calculates missing character types, identifies repeating sequences, and determines necessary changes using minimal operations. If the length exceeds 20, deletions reduce the repeating sequences first.
Complexity
Time Complexity: O(n), where n is the length of the password.
Space Complexity: O(1), in-place computation.
Approach 2: Iterative Character Update
This approach iteratively processes sections of the password and applies necessary operations to resolve length, character type, and repeating character issues, one section at a time.
The C++ method works similarly by processing input iteratively to calculate necessary change operations with attention to minimizing repetitive sections and enforcing character requirements.
Code
C++
JavaScript
Complexity
Time Complexity: O(n), where n is the length of the password.
Space Complexity: O(1), in-place solution.
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Greedy Approach with Replacements | Time Complexity: O(n), where n is the length of the password. |
| Iterative Character Update | Time Complexity: O(n), where n is the length of the password. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Replacement Strategy | O(n) | O(1) | Best overall solution. Handles length constraints and repetition optimally. |
| Iterative Character Update | O(n) | O(1) | Good when implementing logic step‑by‑step in languages like C++ or JavaScript. |
| Heap-Based Greedy Optimization | O(n log n) | O(n) | Useful for visualizing deletion priorities across repeating segments. |
Video Solution
LeetCode 420. Strong Password Checker • Happy Coding • 6,111 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Strong Password Checker easy or hard?
Strong Password Checker Python/Java solution
How to solve Strong Password Checker in O(n)?
What is the best approach for Strong Password Checker?
Is Strong Password Checker asked at Google/Amazon/Meta?
What data structure is used in Strong Password Checker?
What is the time complexity of Strong Password Checker?
Ready to solve this problem?
Practice Strong Password Checker with our built-in code editor and test cases.
Practice on FleetCode