420. Strong Password Checker
Description
Explanation
Observation:
aaa-> del 1 or replace 1aaaa-> del 2 or replace 1aaaaa-> del 3 or replace 1
Therefore, generally we should use more replace to reduce the steps
Denote the input s
- If
len(s) < 6, we have to add6 - len(s) - If
6 <= len(s) && len(s) <= 20, we only replace the right characters - If
len(s) > 20, we have to deletelen(s) - 20any way. Plus, if we have continuous sequence whoselength >= 3. Therefore, we want to use the del to reduce the replace operation.- If we have
aaa, wherelen % 3 == 0, we can reduce 1 replace by using 1 deletion - If we have
aaaa, wherelen % 3 == 1, we can reduce 1 replace by using 2 deletion - If we have
aaaaa, wherelen % 3 == 2, we can reduce 1 replace by using 3 deletion
- If we have