Strong Password Checker

To solve this coding challenge, we need to validate and correct a password based on specific criteria to ensure it is strong. The goal is to minimize the number of steps (insertions, deletions, or replacements) required to achieve this.

# Explanation:

  1. Password Length Requirements : The password must be at least 6 characters and at most 20 characters long.
  2. Character Variety Requirements : The password must contain at least:
    • One lowercase letter
    • One uppercase letter
    • One digit
  3. Repetition Constraint : The password must not have three consecutive repeating characters.
  4. Steps to Solve the Problem :
  5. Initial Checks :
    • Calculate the initial length of the password.
    • Initialize counters for lowercase, uppercase, and digit characters, starting with the assumption that none of these types are present.
    • Track sequences of consecutive repeating characters to handle the repetition constraint.
  6. Analyzing Password :
    • Traverse the password to count lowercase, uppercase letters, digits, and detect sequences of three or more repeating characters.
  7. Determine Required Changes :
    • If the length is less than 6, calculate how many characters need to be added.
    • If the length is more than 20, determine how many characters need to be deleted considering we might still need to make other character changes.
    • Calculate changes needed to satisfy character variety requirements and the avoidance of repeating characters.
  8. Consolidate All Changes :
    • Sum up changes needed for each rule to derive the minimum steps needed.

Step-by-Step Explanation:

1. Initialize necessary variables
Check current password characteristics like presence of lowercase, uppercase, digits, and repetition sequences:
                                            
# Pseudocode to initialize variables:
n = length of password
lower_case_missing = 1   # Assume lowercase is missing initially
upper_case_missing = 1   # Assume uppercase is missing initially
digit_missing = 1        # Assume digit is missing initially

# Create a list to store sequences of repeating characters
repeating_sequences = []
i = 0  # Index to traverse the password

                                        
2. Traverse the password to analyze the character makeup and find repetitions
                                            
# Loop through the password to identify type of characters and sequences:
while i < n:
    if 'a' <= password[i] <= 'z':  
        lower_case_missing = 0  # Found at least one lowercase
    if 'A' <= password[i] <= 'Z':
        upper_case_missing = 0  # Found at least one uppercase
    if '0' <= password[i] <= '9':
        digit_missing = 0       # Found at least one digit

    # Detecting repeating sequences
    start = i
    while i < n and password[i] == password[start]:
        i += 1
    
    length_of_sequence = i - start
    if length_of_sequence >= 3:
        repeating_sequences.append(length_of_sequence)

                                        
3. Determine number of additions or deletions needed
Add/delete based on the password length and required characters:
                                            
# Calculate missing char types:
missing_types = lower_case_missing + upper_case_missing + digit_missing

# Initialization for steps needed
steps_needed = 0

# For a short password less than 6:
if n < 6:
    steps_needed = max(missing_types, 6 - n)

# For a long password greater than 20:
elif n > 20:
    over_len = n - 20
    steps_needed += over_len

    # Modify the repeating sequences to reduce the length first
    for k in range(1, 3):
        for j in range(len(repeating_sequences)):
            if over_len <= 0:
                break
            if repeating_sequences[j] >= 3 and repeating_sequences[j] % 3 == (k - 1):
                removal = min(over_len, k)
                repeating_sequences[j] -= removal
                over_len -= removal

    # Further reduce sequences that are still long
    for i in range(len(repeating_sequences)):
        if repeating_sequences[i] >= 3:
            steps_needed += repeating_sequences[i] // 3

    steps_needed += max(missing_types, steps_needed)

else:
    # For passwords with length between 6 and 20:
    for seq_len in repeating_sequences:
        steps_needed += seq_len // 3
    steps_needed += max(missing_types, steps_needed)

                                        
4. Return the total number of steps calculated
                                            
# Final steps needed to correct the password:
return steps_needed

                                        
The above pseudocode provides an organized and detailed approach to identifying the minimum operations needed to ensure a password meets the predefined strong password criteria. Each step is systematically broken down and explained clearly.