Strong Password HackerRank Solution in C++ - BeTech India!

Latest

BeTech India! - A place for all your Tech. needs | Solutions of Competitive Programming Problems | Code Explained | Indian Engineering System | Technology News | Current Updates | My Excerpts from Experiences | Coding Tips and much more...

Sunday, May 24, 2020

Strong Password HackerRank Solution in C++

PROBLEM  LINK: https://www.hackerrank.com/challenges/strong-password/problem

PROBLEM: Strong Password | HackerRank | Strings | Algorithm | Problem Solving;


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

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.

Constraints

Output Format

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

Sample Input 0

3
Ab1

Sample Output 0

3

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

11
#HackerRank

Sample Output 1

1

Explanation 1

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


C++ Solution Code:

#include <bits/stdc++.h>

using namespace std;
int main()
{
   long long n; string s;
    long long up=0,low=0,digit=0,sp=0;
    cin>>n;
    cin>>s;

    for(int i=0;i<n;i++)
    {
        if(s[i]>='A'&&s[i]<='Z')    up=1;
        else if(s[i]>='a' && s[i]<='z') low=1;
        else if(s[i]>='0' && s[i]<='9') digit=1;
        else sp=1;
    }
    long long ans = (!up)+(!low)+(!digit)+(!sp);
    if(ans+n<6)
    {   
        cout<< ans+(6-n-ans)<<endl;
    }
    else
        cout<<ans<<endl;

    return 0;
}


Comment below for any doubts or questions.

Happy Coding :)

No comments:

Post a Comment

Guys, if you have any doubts or suggestions please let us know.