Advertisement

Strong Password Checker - LeetCode 420 Solution

Strong Password Checker - Complete Solution Guide

Strong Password Checker is LeetCode problem 420, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A password is considered strong if the below conditions are all met: It has at least 6 characters and at most 20 characters. It contains at least one lowercase letter, at least one uppercase letter, and at least one digit . It does not contain three repeating characters in a row (i.e., "B aaa bb0" is weak, but "B aa b a 0" is strong). Given a string password , return the minimum number of steps required to make password strong. if password is already strong, return 0 . In one step, you can: Inse

Detailed Explanation

The problem requires you to determine the minimum number of operations (insertions, deletions, or replacements) needed to transform a given string `password` into a strong password. A strong password must satisfy three conditions: 1) length between 6 and 20 characters, 2) contain at least one lowercase letter, one uppercase letter, and one digit, and 3) not have three consecutive repeating characters. The goal is to find the fewest steps to satisfy all conditions. If the password already meets these criteria, return 0.

Solution Approach

The provided solution employs a greedy approach to minimize the number of operations. It first checks for the presence of lowercase, uppercase, and digit characters and counts the number of missing character types. Then, it identifies repeating character sequences of length 3 or more. Based on the password's length, it applies different strategies: if the password is too short, it prioritizes insertions to reach a length of 6, taking into account any missing character types. If the password is too long, it prioritizes deletions, strategically removing characters from repeating sequences to minimize the number of replacements needed later. If the length is within the acceptable range, it calculates the number of replacements required to eliminate repeating sequences and ensures the password meets the missing character type requirement. The solution efficiently combines insertions, deletions, and replacements to achieve a strong password with the fewest possible operations.

Step-by-Step Algorithm

  1. Step 1: Determine the length of the password and initialize counters for lowercase, uppercase, and digit character types.
  2. Step 2: Check if the password contains at least one lowercase letter, one uppercase letter, and one digit. Count the number of missing character types.
  3. Step 3: Identify all repeating sequences of characters with length >= 3. Store the lengths of these sequences in the `repeats` array/list.
  4. Step 4: If the password length is less than 6, calculate the minimum number of operations needed as the maximum of (6 - password length) and the number of missing character types.
  5. Step 5: If the password length is between 6 and 20 (inclusive), calculate the number of replacements required to fix repeating sequences (r // 3 for each repeating sequence of length r). The minimum number of operations is the maximum of the number of missing character types and the required replacements.
  6. Step 6: If the password length is greater than 20, calculate the number of deletions needed to bring the length down to 20. Prioritize deletions from repeating sequences to minimize replacements. Deletions are processed from repeating sequences where the length mod 3 is 0 first, then sequences where the length mod 3 is 1, and finally, remaining deletions are applied to any sequences. Update the number of necessary replacements accordingly.
  7. Step 7: Return the total number of operations, which is the sum of deletions (if any) and the maximum of missing character types and any remaining replacements.

Key Insights

  • Insight 1: The problem can be broken down into independent subproblems: ensuring sufficient length, fulfilling character type requirements, and handling repeating characters.
  • Insight 2: When the password is too short, insertions are the primary focus. When too long, deletions take precedence. When the length is within bounds, replacements are the main concern for repeating characters.
  • Insight 3: The repeats are handled greedily starting with mod 0 because they have the maximum number of replaceable characters per deletion. then mod 1 for similar reasons

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Greedy, Heap (Priority Queue).

Companies

Asked at: Devtron, Flipkart, Wix.