Strong Password Checker
Real-world version: Strengthening a Vault CodeThe statement here is our own: the same inputs, outputs and technique.
A vault keypad accepts a code as sound only when all four of these hold:
- it is at least 6 and at most 20 characters long;
- it contains at least one lowercase letter;
- it contains at least one uppercase letter;
- it contains at least one digit;
- no character appears three or more times in a row.
In one step an engineer may insert one character anywhere in the code, delete one character from anywhere in it, or overwrite one character with a different one. Inserted and overwritten characters may be any lowercase letter, uppercase letter, digit, dot or exclamation mark.
Given the current code, return the smallest number of steps that leaves it sound. A code that is already sound needs none.
Examples
Example 1
- Input
- code = "Kp7"
- Output
- 3
The code already carries a lowercase letter, an uppercase letter and a digit, and no character repeats, so only its length falls short. Inserting three characters, for instance to reach "Kp7wz4", leaves a sound code.
Example 2
- Input
- code = "aaaB12"
- Output
- 1
The length is allowed and all three kinds are present, but the opening three characters are the same. Overwriting the middle one, for instance to reach "axaB12", leaves a sound code.
Example 3
- Input
- code = "......"
- Output
- 3
The length is allowed, but the code has no lowercase letter, no uppercase letter and no digit, and it carries runs of the same character. Overwriting the second, fourth and sixth characters, for instance to reach ".w.Q.5", leaves a sound code.
Constraints
- 1 <= code.length <= 50
- code consists of letters, digits, dot '.' or exclamation mark '!' only
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def steps_to_strengthen(code: str) -> int:public int stepsToStrengthen(String code)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Reorganize StringReal-world version: Paint Line ShuffleMedium
- Construct String With Repeat LimitReal-world version: Largest Banner Under a Run CapMedium
- Longest Happy StringReal-world version: The Longest Row of Tiles With No TripleMedium
- Lexicographically Minimum String After Removing StarsReal-world version: Clearing the Markers Off the StripMedium