Hackerrank - Strong Password Solution
2 min read

Hackerrank - Strong Password Solution

Hackerrank - Strong Password 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: [email protected]#$%^&*()-+

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"
special_characters = "[email protected]#$%^&*()-+"

Input Format

The first line contains an integer  denoting the length of the string.

The second line contains a string consisting of  characters, the password typed by Louise. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.


Output Format

Print a single line containing a single integer denoting the answer to the problem.

Sample Input 0


Sample Output 0


Explanation 0

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

characters aren't enough since the length must be at least .

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 in Python

import re

def minimumNumber(n, password):
    x = 0
    p = ["[\d]", "[A-Z]", "[a-z]", "[[email protected]#$%^&*()+-]"]
    for i in p:
        if not re.search(i, password):
    return max(6-n,x)

n, password = int(input()), input()
print(minimumNumber(n, password))

Using list comprehension

import re
def minimumNumber(n, password):
    p = ["[\d]", "[A-Z]", "[a-z]", "[[email protected]#$%^&*()+-]"]
    return max(6-n,sum(1 for i in p if not re.search(i, password)))
n, password = int(input()), input()
print(minimumNumber(n, password))

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.