Strong Password Checker in Python

A strong password checker validates if a password meets specific security criteria and calculates the minimum changes needed to make it strong. Python provides several methods to check character types and implement password validation logic.

Password Strength Criteria

A strong password must satisfy these requirements ?

  • Length between 6 and 20 characters
  • At least one lowercase letter, one uppercase letter, and one digit
  • No three consecutive repeating characters (like "aaa", "PPP", "888")

Algorithm Overview

The solution handles three cases based on password length ?

  • Too short (< 6 characters): Add characters or replace existing ones
  • Valid length (6-20 characters): Only replacements needed
  • Too long (> 20 characters): Delete excess characters and optimize replacements

Implementation

def strongPasswordChecker(password):
    # Check missing character types
    missing_types = 3
    if any('a' <= c <= 'z' for c in password): 
        missing_types -= 1
    if any('A' <= c <= 'Z' for c in password): 
        missing_types -= 1
    if any(c.isdigit() for c in password): 
        missing_types -= 1
    
    # Count repeating sequences
    change = 0
    one = two = 0  # Track sequences of length % 3 == 0 and % 3 == 1
    i = 2
    
    while i < len(password):
        if password[i] == password[i-1] == password[i-2]:
            length = 2
            while i < len(password) and password[i] == password[i-1]:
                length += 1
                i += 1
            change += length // 3
            
            if length % 3 == 0: 
                one += 1
            elif length % 3 == 1: 
                two += 1
        else:
            i += 1
    
    # Handle different length cases
    if len(password) < 6:
        return max(missing_types, 6 - len(password))
    elif len(password) <= 20:
        return max(missing_types, change)
    else:
        delete = len(password) - 20
        # Optimize deletions to reduce replacements needed
        change -= min(delete, one)
        change -= min(max(delete - one, 0), two * 2) // 2
        change -= max(delete - one - 2 * two, 0) // 3
        return delete + max(missing_types, change)

# Test the function
test_cases = ["aa26bbb", "aA1", "1337C0d3", "aaaaaaaaaaaaaaaaaaaaa"]

for password in test_cases:
    result = strongPasswordChecker(password)
    print(f"Password: '{password}' ? Changes needed: {result}")
Password: 'aa26bbb' ? Changes needed: 1
Password: 'aA1' ? Changes needed: 3
Password: '1337C0d3' ? Changes needed: 0
Password: 'aaaaaaaaaaaaaaaaaaaaa' ? Changes needed: 2

How It Works

The algorithm follows these steps ?

  1. Character Type Check: Count missing character types (lowercase, uppercase, digits)
  2. Repeating Sequences: Find consecutive repeating characters and calculate replacements needed
  3. Length-based Strategy:
    • Short passwords: Add missing characters
    • Valid length: Replace characters to fix issues
    • Long passwords: Delete excess characters strategically

Example Breakdown

For password "aa26bbb" ?

  • Missing: uppercase letter (1 type missing)
  • Repeating: "bbb" sequence (1 replacement needed)
  • Length: 7 characters (valid range)
  • Solution: Replace one 'b' with uppercase letter ? 1 change total

Conclusion

This strong password checker efficiently calculates minimum changes by analyzing character types and repeating sequences. The algorithm optimizes deletions for long passwords and handles edge cases for different length requirements.

Updated on: 2026-03-25T08:41:15+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements