# 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"
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Â 2.

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

Returns

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

## Constraints

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

Sample Input 0

``````3
Ab1``````

Sample Output 0

``3``

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

``````11
#HackerRank``````

Sample Output 1

``1``

Explanation 1

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

## Solution – Strong Password – HackerRank Solution

### C++

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

{
int con = 0;
//bool digit = false, low = false, upp = false, spec = false;
bool a[4] = {false};

for(int i = 0; i < password.length(); i++){
a[2] = true;
a[1] = true;
a[0] = true;
else
a[3] = true;
}
for(int i = 0; i < 4; i++)
if(!a[i])
con++;

return max(con, 6 - n);
}

int main()
{
int n;
cin >> n;

cout << minimumNumber(n, password) << endl;

return 0;
}
```

### Python

```import math
import os
import random
import re
import sys

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

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

return res

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

n = int(input())

fptr.close()
```

### Java

```import java.io.*;
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(System.in);
int n = in.nextInt();