Tutorialspoint
Problem
Solution
Submissions

Strong Password Checker

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C program to check if a password is strong and return the minimum number of steps required to make it strong. A password is considered strong if it satisfies ALL of the following criteria:

  • 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., "...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).

You can perform the following operations: Insert a character to any position, Delete a character from any position, Replace a character at any position.

Example 1
  • Input: password = "a"
  • Output: 5
  • Explanation:
    • Password length is 1, needs at least 6 characters.
    • Missing uppercase letter and digit.
    • Need to add 5 characters to reach minimum length while satisfying missing type requirements.
Example 2
  • Input: password = "aaaaaaaaaaaaaaaaaaaaa"
  • Output: 7
  • Explanation:
    • Password length is 21, exceeds maximum of 20.
    • Has lowercase but missing uppercase and digit.
    • Has repeating characters "aaa" multiple times.
    • Need to delete 1 character and replace others to fix issues.
Constraints
  • 1 ≤ password.length ≤ 50
  • password consists of only English letters and digits
  • You can perform insert, delete, or replace operations
  • Time Complexity: O(n) where n is password length
  • Space Complexity: O(1)
StringsDeloitteLTIMindtree
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

  • Check the length first - if too short, calculate additions needed
  • If too long, calculate deletions needed and prioritize fixing repeating sequences
  • Count missing character types (lowercase, uppercase, digit)
  • Handle repeating sequences by counting groups of 3+ consecutive characters
  • For short passwords, additions can fix both length and missing types
  • For long passwords, deletions and replacements should be optimized
  • Use greedy approach to minimize total operations

Steps to solve by this approach:

 Step 1: Calculate password length and check if it's too short (< 6), good (6-20), or too long (> 20)

 Step 2: Iterate through password to identify missing character types (lowercase, uppercase, digit)
 Step 3: Count repeating character sequences of 3 or more consecutive identical characters
 Step 4: For short passwords, calculate additions needed and check if they can satisfy missing types
 Step 5: For good length passwords, return maximum of repeating sequences and missing types
 Step 6: For long passwords, calculate deletions needed and adjust repeating sequences accordingly
 Step 7: Return minimum total operations considering all constraints and optimization strategies

Submitted Code :