All problems
0655HardStringGreedyHeap (Priority Queue)

Strengthening a Vault Code

Tracked in this browser only
Write code

Trains the technique from

LeetCode 420Strong Password Checker

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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