A password is considered strong if the below conditions are all met:
It has at least 6 characters and at most 20 characters.
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 password 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 <= 50
password consists of letters, digits, dot '.' or exclamation mark '!'.
Solutions
Solution 1
Thinking
A strong password constrains length, character classes, and runs of three identical letters. Insert, delete, and replace help those gaps differently, so one operation type is not enough.
Split by length. If \(n<6\), inserts cover both length and missing classes. If \(6\le n\le 20\), replacements break runs of length \(3\), then take the max with missing classes. If \(n>20\), deletions are mandatory; spend them first on runs whose length is \(0\bmod 3\), because one delete removes one later replacement.
The case split isolates the operation that is actually required for each length.