Watch 10 video solutions for Strong Password Checker, a hard level problem involving String, Greedy, Heap (Priority Queue). This walkthrough by Gohar Khan has 12,372,080 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A password is considered strong if the below conditions are all met:
6 characters and at most 20 characters."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:
password,password, orpassword with 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 <= 50password consists of letters, digits, dot '.' or exclamation mark '!'.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 | 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. |