Ctrl K

Harden the Password

Hard
A security policy treats a password as strong when all of these hold: - it is between 6 and 20 characters long, inclusive; - it has at least one lowercase English letter, at least one uppercase letter and at least one digit; - it never has three equal characters in a row (xAaaa1 is not allowed, xAaxa1 is fine). One edit inserts a character, deletes a character or replaces one character with another. Given the string password, return the smallest number of edits that makes it strong. If it is strong already, return 0.
Example 1
password='ab' → 4

The password is 4 characters short of six, and it also lacks an uppercase letter and a digit. Four insertions fix everything at once, for example abC12x.

Example 2
password='Qwerty1111' → 1

Length and character kinds are fine, but four 1s in a row break the rule. Replacing one of them is enough: Qwerty11x1.

Example 3
password='Spring.Day.2026' → 0

The password is already strong.

Constraints
  • 1 ≤ password.length ≤ 100
  • password consists of printable ASCII characters other than space (codes 33 to 126)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEqual Letters Kept Apart HardNo Twin Letters Side by Side MediumThree Letters, No Triples MediumLargest String With a Run Cap MediumFill the Blanks With the Fewest Repeats Medium
loading Python…
Input
password='ab'
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 177 tests on the server, hidden ones included.