
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:
- It has at least 6 characters and at most 20 characters.
- It contains at least one lowercase letter, one uppercase letter, and one digit.
- 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)
Editorial
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. |
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.