본문 바로가기
개발/코딩

해커랭크(HackerRank) - Strong Password / C++

by lucidmaj7 2020. 6. 30.
728x90
반응형

문제 : 해커랭크 - Strong Password 

난이도 : easy

언어 : C++

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

 

Strong Password | HackerRank

How many characters should you add to make the password strong?

www.hackerrank.com

 

// Complete the minimumNumber function below.
int minimumNumber(int n, string password) {
    // Return the minimum number of characters to make the password strong
    string numbers = "0123456789";
    string lower_case = "abcdefghijklmnopqrstuvwxyz";
    string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string special_characters = "!@#$%^&*()-+";

    int passwordLength = password.length();
    int countLower = 0;
    int countUpper = 0;
    int countSpecialChar = 0;
    int countNumber = 0;
    int addreq = 0;
    for (int i = 0; i < passwordLength; i++)
    {
        if (numbers.find(password.at(i)) != -1)
        {
            countNumber++;
            continue;
        }
        if (lower_case.find(password.at(i)) != -1)
        {
            countLower++;
            continue;
        }
         if (upper_case.find(password.at(i)) != -1)
        {
            countUpper++;
            continue;
        }
        if (special_characters.find(password.at(i)) != -1)
        {
            countSpecialChar++;
            continue;
        }
    }

    if (countNumber == 0)
    {
        addreq++;
    }

    if (countLower == 0)
    {
        addreq++;

    }
    if (countUpper == 0)
    {
        addreq++;
        
    }
    if (countSpecialChar == 0)
    {
        addreq++;
    
    }

    if (6- passwordLength > addreq)
    {
        return  6- passwordLength;
    }
    return addreq;
}

728x90
반응형

댓글