Show problems as
Classic problem #420HardStringGreedyHeap (Priority Queue)

Strong Password Checker

Real-world version: Strengthening a Vault Code
Tracked in this browser only
Read Strong Password Checker on LeetCode(opens in a new tab)

The 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.

Python
def steps_to_strengthen(code: str) -> int:
Java
public int stepsToStrengthen(String code)