Problem Description
A password is considered strong if it meets all of the following conditions:
- It has between 6 and 20 characters inclusive.
- 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.
Given a string password, return the minimum number of steps required to transform password into a strong one. In one step, you can insert, delete, or replace a character.
Key Insights
- Check for missing character types (lowercase, uppercase, digit).
- Count groups of consecutive repeating characters to detect where replacements are needed.
- For short passwords (less than 6), the answer is the maximum between missing character types and the number of insertions required.
- For passwords of acceptable length (6 to 20), focus on performing replacements for groups of repeating characters.
- For long passwords (more than 20), deletions are required, and strategically deleting characters within repeating groups (especially using mod 3 heuristics) can reduce the number of replacements required.
- The overall minimum steps are influenced by a combination of deletions (if necessary), replacements, and insertions to fix missing character types.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the password
Space Complexity: O(n) in the worst-case (for storing information about repeating groups)
Solution
The solution works in multiple phases:
- Count missing character types.
- Identify consecutive repeating sequences and determine how many replacements are needed if no deletions occur.
- Handle cases:
- If the password is too short, return the maximum between missing character types and the number of insertions needed (6 - length).
- If the password has valid length (6 to 20), the answer is max(missing types, required replacements for repeating groups).
- If the password is too long (over 20), first compute the number of deletions needed. Then, use deletions in a greedy manner on repeating groups to reduce the replacements by targeting groups with lengths that benefit most from a deletion (those with length % 3 == 0, then % 3 == 1, and so on). Finally, add the number of deletions to the maximum of the missing character types and the adjusted replacement count.
Data structures include arrays/lists to track repeating groups. The approach combines greedy deletions with character type and replacement counting.