> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Strong Password Checker Python Solution

> Tested Python solution for LeetCode 420 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 420, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Greedy](/catalog/topics/greedy), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/strong-password-checker/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 420   # by problem number
lcpy gen -s strong_password_checker   # by problem name
```

## Problem

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., `"B**aaa**bb0"` is weak, but `"B**aa**b**a**0"` 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.

### Examples

```
Input: password = "a"
Output: 5
```

```
Input: password = "aA1"
Output: 3
```

```
Input: password = "1337C0d3"
Output: 0
```

### Constraints

* 1 \<= password.length \<= 50
* password consists of letters, digits, dot '.' or exclamation mark '!'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/strong_password_checker/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/strong_password_checker/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def strong_password_checker(self, password: str) -> int:
        n = len(password)
        missing = 3 - (
            any(c.islower() for c in password)
            + any(c.isupper() for c in password)
            + any(c.isdigit() for c in password)
        )

        runs: list[int] = []
        i = 0
        while i < n:
            j = i
            while j < n and password[j] == password[i]:
                j += 1
            runs.append(j - i)
            i = j

        if n < 6:
            # Insertions cover both the length gap and the missing types.
            return max(6 - n, missing)

        replace = sum(run // 3 for run in runs)
        if n <= 20:
            # A replacement fixes a missing type and breaks a repeat at once.
            return max(missing, replace)

        # Length must shrink to 20; spend deletions where they save a replacement.
        delete = n - 20
        lengths = runs[:]
        remaining = delete
        for mod in (0, 1):
            for idx, run in enumerate(lengths):
                if remaining <= 0:
                    break
                if run >= 3 and run % 3 == mod:
                    spent = min(remaining, mod + 1)
                    lengths[idx] -= spent
                    remaining -= spent
            if remaining <= 0:
                break

        replace_left = sum(run // 3 for run in lengths)
        # Leftover deletions still help: every 3 of them shorten a run past one repeat.
        return delete + max(missing, replace_left - remaining // 3)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.