
Ninjas are trying to hack a system of a terrorist organization so that they can know where they will be going to attack next. But to hack the system and to get access to data they need a password that must satisfy certain conditions as described below:
1) Length of the password must be between 8 to 15 characters.
2) At least one digit (0-9), one lowercase letter (a-z), one uppercase letter (A-Z) and one special character (%, ^, &, #, *, %, etc) must be present.
3) Password must not contain any space.
You are given a string ‘STR’, help ninjas to find whether it's a valid password or not.
For example :
Given ‘STR’ = “Codingninja#1” As it satisfies all the above conditions so it is a valid password and therefore you have to return true.
Input Format
The first line of input contains an integer 'T' representing the number of test cases.
The description of the next ‘T’ lines is as follows-.
The first and the only line of each test case contains a single string 'STR' representing the password.
Output Format:
For each test case, return “Valid” if the given string ‘STR’ satisfies all the above conditions and “Not Valid” if it's not.
The output of each test case will be printed in a separate line.
Constraints:
1 <= T <= 5
1 <= |STR| <= 5*10^3
Time Limit: 1 second
3
CODiNGNinja+1
abcXyz 123
itsnotValid1
Valid
Not Valid
Not Valid
Test Case 1 :
Given ‘STR’ = CODiNGNinja+1
As we can see that this string satisfies all the given conditions in the problem.
Therefore, it's a valid password.
Test Case 2 :
Given ‘STR’ = abcXyz 123
As the given string contains a space, so it's not a valid password.
Test Case 3:
Given ‘STR’ = itsnotValid1
As the string does not contain any special character, so it’s not a valid password.
3
&1CodingISBest
Ab@3
HEllo@World#0
Valid
Not Valid
Valid
Try to check conditions with the help of ASCII Codes.
The idea here is we will check all conditions one by one, if any condition fails at any instance we will directly return false and output Not valid password else we will output valid.
Every character has a particular number known as ASCII code so we will use the ASCII code to check whether all conditions are satisfied or not.
In the below table there are ASCII codes given of the required characters.
| Ascii Code | Characters |
| 48 - 57 | Digits (0-9) |
| 65 - 90 | Uppercase (A-Z) |
| 97 - 122 | Lowercase (a-z) |
| 33 - 47, 58 - 64, 91 - 96, 123 - 126 | Special Characters |
For a valid password, we need to check at least one character from each row must be present.
Algorithm:
If str[i] is equal to ‘ ‘(space)
'CONTAIN_SPACE’ = 1;
Check for all above ASCII code and change the corresponding variable.
O(N), where ‘N’ is the length of the string.
In the worst case, we need to check all characters of the string and so we need to traverse the whole string therefore the net time complexity will be O(N) .
O(1).
Since we are not using any extra space to store anything.