Strong Password

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:

1.Its length is at least 6.

2.It contains at least one digit.

3.It contains at least one lowercase English character.

4.It contains at least one uppercase English character.

5.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?


Example:

Input:  n=3, s="Ab1"
Output: 3

Approach

Java


public class StrongPassword {
    public static void main(String[] args) {
        int n = 3;
        String password = "Ab1";

        int answer = minimumNumber(n, password);
        System.out.println(answer);
    }

    private static int minimumNumber(int nString password) {

        int cnt0 = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0;
        for (int i = 0; i < n; i++) {
            if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
                cnt2++;
            if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
                cnt1++;
            if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
                cnt0++;
            if (password.charAt(i) == '!' || password.charAt(i) == '@' || password.charAt(i) == '#'
                    || password.charAt(i) == '$' || password.charAt(i) == '^' || password.charAt(i) == '&'
                    || password.charAt(i) == '*' || password.charAt(i) == '(' || password.charAt(i) == ')'
                    || password.charAt(i) == '-' || password.charAt(i) == '+')
                cnt3++;
        }

        int sum = 0;
        if (cnt0 == 0)
            sum += 1;
        if (cnt1 == 0)
            sum += 1;
        if (cnt2 == 0)
            sum += 1;
        if (cnt3 == 0)
            sum += 1;
        if (sum + password.length() < 6)
            return 6 - password.length();
        return sum;
    }
}


C++

#include <bits/stdc++.h>

using namespace std;

int minimumNumber(int nstring password)
{
    int cnt0 = 0cnt1 = 0cnt2 = 0cnt3 = 0;
    for (int i = 0i < ni++)
    {
        if (password[i] >= 'A' && password[i] <= 'Z')
            cnt2++;
        if (password[i] >= 'a' && password[i] <= 'z')
            cnt1++;
        if (password[i] >= '0' && password[i] <= '9')
            cnt0++;
        if (password[i] == '!' || password[i] == '@' || 
password[i] == '#' || password[i] == '$' ||
 password[i] == '^' || password[i] == '&' || 
password[i] == '*' || password[i] == '(' || 
password[i] == ')' || password[i] == '-' || password[i] == '+')
            cnt3++;
    }

    int sum = 0;
    if (cnt0 == 0)
        sum += 1;
    if (cnt1 == 0)
        sum += 1;
    if (cnt2 == 0)
        sum += 1;
    if (cnt3 == 0)
        sum += 1;
    if (sum + password.size() < 6)
        return 6 - password.size();
    return sum;
}

int main()
{
    int n = 3;
    string password = "Ab1";

    int answer = minimumNumber(npassword);
    cout << answer << "\n";

    return 0;
}


No comments:

Post a Comment