Strong Password Checker II

We will check each condition one by one and make sure the password meets all of them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
    def strongPasswordCheckerII(self, password: str) -> bool:
        # Check length
        if len(password) < 8:
            return False

        # Check for lowercase letter
        if not any(c.islower() for c in password):
            return False

        # Check for uppercase letter
        if not any(c.isupper() for c in password):
            return False

        # Check for digit
        if not any(c.isdigit() for c in password):
            return False

        # Check for special character
        if not any(c in "!@#$%^&*()-+" for c in password):
            return False

        # Check for adjacent repetition
        for i in range(1, len(password)):
            if password[i] == password[i-1]:
                return False

        return True

In this function, we are making use of Python’s built-in string methods like islower(), isupper(), isdigit(), and the any() function to check the various conditions.

We’re also using a loop to check for any adjacent characters that are the same.

If any of these conditions fail, we immediately return False. If all conditions pass, we return True.