Enhancing Password Strength

Problem Statement

We are given a task to enhance the strength of a password to meet specific criteria. A password is considered strong if it satisfies the following conditions:

  • It must be at least 6 characters and, at most, 20 characters long.
  • It should include at least one lowercase letter, one uppercase letter, and one digit.
  • It must not contain three consecutive repeating characters.

Your goal is to determine the minimum number of steps required to make the given password strong. If the password already meets the criteria, return 0.

In a single step, you are allowed to perform one of the following actions:

  • Insert one character into the password.
  • Delete one character from the password.
  • Replace one character in the password with another character.

Provide a function that takes the current password as input and returns the minimum number of steps needed to strengthen it.

Java Implementation of above approach

Java Approach 1

Output:

Enter the password: p@ssw0rd123
Minimum steps to make the password strong: 0

Code Explanation:

The Above Java code aims to determine the minimum number of steps required to strengthen a password based on specific criteria. The conditions for a strong password include having a length between 6 and 20 characters, containing at least one lowercase letter, one uppercase letter, and one digit, and avoiding three consecutive repeating characters.

Iterative Character Analysis:

The code uses a character array to analyze the password iteratively. It keeps track of the counts of lowercase letters (a), uppercase letters (A), and digits (d). Additionally, it identifies consecutive repeating characters and stores their counts in an array (arr).

Password Length Check:

If the password length is less than 6, the code calculates the total missing elements (lowercase, uppercase, digit) and adds the necessary characters to meet the minimum length requirement (6). If the password is longer than 6 characters, the code addresses the possibility of exceeding the maximum length (20).

Adjusting Password Length:

If the password length exceeds 20 characters, the code calculates the excess characters (over_len). It then proceeds to adjust the length by removing characters.

Handling Consecutive Repeating Characters:

The code iterates through the consecutive repeating character counts (arr). It strategically reduces the counts to avoid three consecutive repeating characters, minimizing the overall steps required.

Calculating Remaining Steps:

After adjusting the length and handling repeating characters, the code calculates the remaining steps needed to satisfy the strong password conditions. It considers missing lowercase, uppercase, and digit characters, as well as any remaining characters that could be added to fulfill the criteria.

Time Complexity Explanation :

The time complexity of the code is O(N), where N is the length of the input password.






Latest Courses