Strong Password Checker II — LeetCode 2299 Python Solution

EasyString
Problem
#2299
Pattern
Hash Map
Reading time
3 min

The problem

A password is said to be strong if it satisfies all the following criteria: It has at least 8 characters. It contains at least one lowercase letter.

Example

Input
password = "IloveLe3tcode!"
Output
true
Explanation
The password meets all the requirements. Therefore, we return true.

Python solution

Python
class Solution:
    def strongPasswordCheckerII(self, password: str) -> bool:
        if len(password) < 8:
            return False
        mask = 0
        for i, c in enumerate(password):
            if i and c == password[i - 1]:
                return False
            if c.islower():
                mask |= 1
            elif c.isupper():
                mask |= 2
            elif c.isdigit():
                mask |= 4
            else:
                mask |= 8
        return mask == 15

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2299. Strong Password Checker II is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2299. Strong Password Checker II?
LeetCode 2299. Strong Password Checker II is rated Easy on LeetCode.
What is the time complexity of LeetCode 2299. Strong Password Checker II?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2299. Strong Password Checker II?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2299. Strong Password Checker II cover?
LeetCode 2299. Strong Password Checker II is tagged String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview