Passwords should be easy to remember but difficult to hack or guess. According to Ninja Cyber Security Centre, a password should have a minimum of 6 lengths and a maximum of 20 lengths. It should have at least one lowercase letter, one uppercase letter, and one digit. The most important condition is that the password should not contain any repeating characters of length more than 2.
Given a string ‘STR’ as a password, find the minimum number of changes we need to make to strengthen that password. We can insert a character, delete a character and replace a character from the password at a time.
The first line of input contains an integer ‘T’, representing the number of test cases.
The first line of each test case contains a string ‘STR’, representing the password.
Output Format :
For each test case, print a single integer, representing the number of changes we need to make.
Output for each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |STR| <= 5000
Where |STR| is the length of the given string ‘STR’.
Time Limit : 1 sec
2
a
abcdefg
5
2
For first test case :
Given password : a
We have to do a minimum of 5 changes to the password to make its length 6.
Possible password : aAbBc1
Number of changes done : 5
For second test case :
Given password : abcdefg
We need one uppercase and one digit to strengthen the password.
Possible password : Abcdef1
Number of changes done : 2
2
1337C0d
aA1
0
3
Greedily check for all the conditions.
The idea is to check for all the conditions greedily. We need to check whether the password consists of at least one lowercase letter, one uppercase, and one digit and no repeated characters of length more than 2. There are 3 possible combinations, i.e., ‘N’ < 6, ‘N’ <= 20 and ‘N’ > 20.
For ‘N’ < 6 , we can insert the missing conditions.
For ‘N’ <= 20, we can replace the necessary characters.
For ‘N’ > 20, we can do combinations of deletions and replacements.
Example:
bbbbbb: In this, we can delete the last character and do 1 replacement.
bbbbbbb: In this, we can do 2 deletions to save 1 replacement.
bbbbbbbb: In this, we can do 3 deletions to save 1 replacement.
Here is the algorithm :
O(N), where ‘N’ is the length of the string.
We traverse the string once to check for the conditions. Therefore, the overall time complexity will be O(N).
O(1)
We are not using any extra space. Therefore, the overall space complexity will be O(1).