Tutorialspoint
Problem
Solution
Submissions

Strong Password Checker

Certification: Advanced Level Accuracy: 50% Submissions: 2 Points: 15

Write a Java program to determine the minimum number of changes required to make a password strong. A password is considered strong if it satisfies all of the following criteria:

  1. It has at least 6 characters and at most 20 characters.
  2. It contains at least one lowercase letter, one uppercase letter, and one digit.
  3. It does not contain three repeating characters in a row (i.e., "...aaa..." is weak, but "...aba..." is strong).

You can perform the following operations on the password:

  • Insert one character
  • Delete one character
  • Replace one character

Return the minimum number of operations needed to make the password strong.

Example 1
  • Input: password = "a"
  • Output: 5
  • Explanation:
    • Step 1: The password "a" is only 1 character long, which is less than the minimum required length of 6.
    • Step 2: We need to add at least 5 more characters to reach the minimum length.
    • Step 3: We also need to ensure it has at least one digit and one uppercase letter.
    • Step 4: One optimal solution is to add characters to make it "aA1bcd", which requires 5 insertions.
    • Step 5: This modified password is now strong as it meets all the criteria.
Example 2
  • Input: password = "aA1abcabcabc"
  • Output: 1
  • Explanation:
    • Step 1: The password has 12 characters, which is between 6 and 20, so length is valid.
    • Step 2: It contains lowercase 'a', uppercase 'A', and digit '1', so character types are satisfied.
    • Step 3: However, it contains "abc" three times in a row, forming a repeating pattern.
    • Step 4: We need to change one character to break this pattern.
    • Step 5: For example, changing one character to make it "aA1abcabXabc" requires 1 operation.
Constraints
  • 1 ≤ password.length ≤ 50
  • password consists of lowercase and uppercase English letters and digits
  • Time Complexity: O(n)
  • Space Complexity: O(1)
StringsIBMDropbox
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Handle the three cases separately: length check, character type check, and repeating character check.
  • For length, if it's less than 6, insertion is needed; if it's more than 20, deletion is needed.
  • For character types, check if the password is missing lowercase, uppercase, or digit.
  • For repeating characters, identify sequences of three or more identical characters.
  • Combine these requirements in an optimal way to minimize the total number of operations.

Steps to solve by this approach:

 Step 1: Check if the password contains lowercase letters, uppercase letters, and digits. Count how many of these types are missing.

 Step 2: Identify all sequences of repeating characters (three or more identical characters in a row) and calculate how many replacements would be needed to fix them.
 Step 3: Separate cases based on the password length:
 Step 4: If the length is less than 6, we need to add characters. We can simultaneously fix missing character types while adding characters, so the answer is max(6 - length, missing types).
 Step 5: If the length is between 6 and 20, we need to ensure there are no repeating sequences and all character types are present. The answer is max(replacements needed, missing types).
 Step 6: If the length is greater than 20, we must delete (length - 20) characters. We can strategically delete characters from repeating sequences to minimize the number of replacements needed afterward.
 Step 7: Return the total number of operations: deletions + max(remaining replacements, missing types).

Submitted Code :