HackerRank 'Strong Password' Solution

by Srikant Padala on March 25, 2018, 12:15 pm


Explanation


  • Keep a flag for each criteria.
  • Iterate over each character in the supplied password, check if satisfies some criteria and increment the answer by 1.
  • Finally increment the answer so that it becomes atleast 6 characters.

Strong Password Problem Statement

Video

#include <bits/stdc++.h>

using namespace std;

int minimumNumber(int n, string password) {
    // Return the minimum number of characters to make the password strong
    int len = password.size();
    string specials = "!@#$%^&*()-+";
    bool small = false, caps = false, numeric = false, special = false;
    for(int i=0; i < len; i++) {
        if(password[i] >= 'a' && password[i] <= 'z')
            small = true;
        if(password[i] >= 'A' && password[i] <= 'Z')
            caps = true;
        if(password[i] >= '0' && password[i] <= '9')
            numeric = true;
        if(specials.find(password[i]) != string::npos)
            special = true;
    }
    int ans = 0;
    if(!small) ans++;
    if(!caps) ans++;
    if(!numeric) ans++;
    if(!special) ans++;
    if(len + ans < 6) ans+= 6-(len+ans);
    return ans;
}

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

Coming Soon.