Strong Password – HackerRank Solution

In this post, we will solve Strong Password HackerRank Solution. This problem (Strong Password) is a part of HackerRank Problem Solving series.


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 6.
  • 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 n 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"
special_characters = "!@#$%^&*()-+"


password = ‘2bbbb’

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

password = ‘2bb#A’

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

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


  • int: the minimum number of characters to add

Input Format

The first line contains an integer n, 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.


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

Sample Input 0


Sample Output 0


Explanation 0

She can make the password strong by adding 3 characters, for example, $hk, turning the password into Ab1$hk which is strong.

2 characters aren’t enough since the length must be at least 6.

Sample Input 1


Sample Output 1


Explanation 1

The password isn’t strong, but she can make it strong by adding a single digit.

Solution – Strong Password – HackerRank Solution


#include <bits/stdc++.h>
using namespace std;

int minimumNumber(int n, string password)
    int con = 0;
    //bool digit = false, low = false, upp = false, spec = false;
    bool a[4] = {false};
    for(int i = 0; i < password.length(); i++){
        if(password[i] >= 'A' && password[i] <= 'Z')
            a[2] = true;
        else if(password[i] >= 'a' && password[i] <= 'z')
            a[1] = true;
        else if(password[i] >= '0' && password[i] <= '9')
            a[0] = true;
            a[3] = true;
    for(int i = 0; i < 4; i++)
    return max(con, 6 - n);

int main()
    int n;
    cin >> n;
    string password;
    cin >> password;
    cout << minimumNumber(n, password) << endl;
    return 0;


import math
import os
import random
import re
import sys

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

def minimumNumber(n, password):
    res = 0

    if not any(x in numbers for x in password):
        res += 1
    if not any(x in lower_case for x in password):
        res += 1

    if not any(x in upper_case for x in password):
        res += 1

    if not any(x in special_characters for x in password):
        res += 1

    if len(password) < 6:
        return max(res, 6 - len(password))
    return res

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())
    password = input()

    answer = minimumNumber(n, password)

    fptr.write(str(answer) + '\n')


import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int minimumNumber(int n, String pwd) {
        if(n<=3) return 6-n;
        boolean num = false, lower = false, upper = false, special = false;
        for(char c : pwd.toCharArray()){
            if(isNum(c)) num = true;
            else if(isLower(c)) lower = true;
            else if(isUpper(c)) upper = true;
            else special = true;
        boolean length = (n>=6);
        int count = 0;
        if(!num) count++;
        if(!lower) count++;
        if(!upper) count++;
        if(!special) count++;
        return (count+n < 6) ? 6-n : count;
    static boolean isNum(char c){
        return (c>='0' && c<='9');
    static boolean isLower(char c){
        return (c>='a' && c<='z');
    static boolean isUpper(char c){
        return (c>='A' && c<='Z');

    public static void main(String[] args) {
        Scanner in = new Scanner(;
        int n = in.nextInt();
        String password =;
        int answer = minimumNumber(n, password);

Note: This problem (Strong Password) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *