Strong Password HackerRank Solution

In this Strong Password HackerRank solution, Louise joined a social networking site to stay in touch with her friends. The signup page required her to input a name and a password. However, the password must be strong. The website considers a password to be strong if it satisfies the following criteria:

  • Its length is at least .
  • It contains at least one digit.
  • It contains at least one lowercase English character.
  • It contains at least one uppercase English character.
  • It contains at least one special character. The special characters are: !@#$%^&*()-+

She typed a random string of length  in the password field but wasn’t sure if it was strong. Given the string she typed, can you find the minimum number of characters she must add to make her password strong?

Note: Here’s the set of types of characters in a form you can paste in your solution:

numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"

Example

This password is 5 characters long and is missing an uppercase and a special character. The minimum number of characters to add is .

This password is 5 characters long and has at least one of each character type. The minimum number of characters to add is .

Function Description

Complete the minimumNumber function in the editor below.

minimumNumber has the following parameters:

  • int n: the length of the password
  • string password: the password to test

Returns

  • int: the minimum number of characters to add

Input Format

The first line contains an integer , the length of the password.

The second line contains the password string. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.

Constraints

  • All characters in  are in [a-z], [A-Z], [0-9], or [!@#$%^&*()-+ ].

Strong Password HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in Python

def minimumNumber(n, password):
    scores = [1]*4
   
    for i in range(n):
        if password[i].isupper():
            scores[0] = 0
        elif password[i].islower():
            scores[1] = 0
        elif password[i].isnumeric():
            scores[2] = 0
        elif password[i] in "!@#$%^&*()-+":
            scores[3] = 0
   
    return max(sum(scores), 6 - n)

Problem Solution in JavaScript

function minimumNumber(n, password) {
    let numbers = "0123456789";
    let lower_case = "abcdefghijklmnopqrstuvwxyz";
    let upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let special_characters = "!@#$%^&*()-+";
    let score = 4;
    for (let i = 0; i<password.length; i++){
        if(numbers.includes(password[i])){
            score --;
            numbers = "";
        } else if (lower_case.includes(password[i])){
            score --;
            lower_case= "";
        } else if (upper_case.includes(password[i])){
            score --;
            upper_case= "";
        } else if (special_characters.includes(password[i])){
            score --;
            special_characters= "";
        }
    }
    if(score+password.length>=6){
        return score;
    } else {
        return 6-password.length
    }  
}

Problem Solution in C++

int minimumNumber(int n, string password) {    
    string numbers = "0123456789";
    string lower_case = "abcdefghijklmnopqrstuvwxyz";
    string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string special_characters = "!@#$%^&*()-+";    
   
    bool has_n, has_l, has_u, has_s;
    has_n = has_l = has_u = has_s = false;
   
    for (const auto & letter : password)
    {
        if (!has_n && numbers.find(letter) != string::npos)        
            has_n = true;            
        if (!has_l && lower_case.find(letter) != string::npos)        
            has_l = true;
        if (!has_u && upper_case.find(letter) != string::npos)        
            has_u = true;
        if (!has_s && special_characters.find(letter) != string::npos)        
            has_s = true;
    }
   
    int rest_chars = !has_n + !has_l + !has_u + !has_s;
   
    return (rest_chars < (int)(6 - password.size()) ? 6 - password.size() : rest_chars);        
}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment