Program to check the validity of a Password

Easy
0/40
Average time to solve is 20m
52 upvotes
Asked in companies
UHG314e Corporation

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )

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

Sample Input 1:

3
CODiNGNinja+1
abcXyz 123
itsnotValid1

Sample Output 1:

Valid
Not Valid
Not Valid

Explanation of Sample Input 1:

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.

Sample Input 2:

3
&1CodingISBest
Ab@3
HEllo@World#0

Sample Output 2:

Valid
Not Valid
Valid
Hint

Try to check conditions with the help of ASCII Codes.

Approaches (2)
Conditional Implementation

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:

  • Declare 6 variables ‘N’= STR.length, ‘CONTAIN_DIGIT’ = 0, ‘CONTAIN_UPPER’ = 0, ‘CONTAIN_LOWER’ = 0, ‘CONTAIN_SPECIAL’ = 0, ‘CONTAIN_SPACE’ = 0.
  • Check if ‘N’ is between 8 and 15. If it's not then return false.
  • Run a loop from i = 0 to ‘N’. Here ‘N’ is the length of the string.

             If str[i] is equal to  ‘ ‘(space)   

                  'CONTAIN_SPACE’ = 1;

Check for all above ASCII code and change the corresponding variable.

 

  • Return true if(‘CONTAIN_SPACE’ = 0 and ‘CONTAIN_DIGIT’ = 1 and ‘CONTAIN_UPPER’ = 1 and ‘CONTAIN_LOWER’ = 1 and ‘CONTAIN_SPECIAL’ = 1) else return false.
Time Complexity

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) . 

Space Complexity

O(1).

 

Since  we are not using any extra space to store anything.

Code Solution
(100% EXP penalty)
Program to check the validity of a Password
Full screen
Console