Back to blog
Jun 03, 2024
4 min read

Strong Password Checker II

Check if a password is strong based on length, character types, and no consecutive identical characters.

Difficulty: Easy | Acceptance: 55.40% | Paid: No Topics: String

A password is said to be strong if the below conditions are all met:

  • It has at least 8 characters.
  • It contains at least one lowercase letter.
  • It contains at least one uppercase letter.
  • It contains at least one digit.
  • It contains at least one special character. The special characters are: !@#$%^&*()-+
  • It does not contain two consecutive identical characters.

Given a string password, return true if it is a strong password. Otherwise, return false.

Examples

Example 1

Input: password = "IloveLe3tcode!"
Output: true
Explanation: The password meets all the requirements.

Example 2

Input: password = "Me+You--IsMyDream"
Output: false
Explanation: The password does not contain a digit and has consecutive identical characters.

Example 3

Input: password = "1aB!"
Output: false
Explanation: The password is less than 8 characters.

Constraints

1 <= password.length <= 100
password consists of letters, digits, and special characters: !@#$%^&*()-+

Single Pass Validation

Intuition Iterate through the password once, tracking which criteria are met and checking for consecutive identical characters.

Steps

  • Check if length is at least 8
  • Initialize flags for lowercase, uppercase, digit, and special character
  • Iterate through each character:
    • Update appropriate flags based on character type
    • Check if current character equals previous character
  • Return true only if all flags are true and no consecutive duplicates found
python
class Solution:
    def isStrongPassword(self, password: str) -> bool:
        if len(password) < 8:
            return False
        
        has_lower = False
        has_upper = False
        has_digit = False
        has_special = False
        special = set("!@#$%^&*()-+")
        
        for i, ch in enumerate(password):
            if ch.islower():
                has_lower = True
            elif ch.isupper():
                has_upper = True
            elif ch.isdigit():
                has_digit = True
            elif ch in special:
                has_special = True
            
            if i > 0 and ch == password[i - 1]:
                return False
        
        return has_lower and has_upper and has_digit and has_special

Complexity

  • Time: O(n) where n is the length of the password
  • Space: O(1) - only using a constant number of boolean variables
  • Notes: This is the most efficient approach with a single pass through the string

Regular Expression

Intuition Use regular expressions to check each requirement separately, combining all conditions.

Steps

  • Check length requirement
  • Use regex patterns for lowercase, uppercase, digit, and special character
  • Use regex to check for consecutive identical characters
  • Combine all checks with logical AND
python
import re

class Solution:
    def isStrongPassword(self, password: str) -> bool:
        if len(password) < 8:
            return False
        
        has_lower = bool(re.search(r'[a-z]', password))
        has_upper = bool(re.search(r'[A-Z]', password))
        has_digit = bool(re.search(r'\d', password))
        has_special = bool(re.search(r'[!@#$%^&*()\-+]', password))
        no_consecutive = not bool(re.search(r'(.)\1', password))
        
        return has_lower and has_upper and has_digit and has_special and no_consecutive

Complexity

  • Time: O(n) where n is the length of the password (each regex scan is O(n))
  • Space: O(1) - regex patterns are constant size
  • Notes: More readable but may be slightly slower due to multiple regex passes