K-D²

Pangrams 본문

Algorithm/HackerRank

Pangrams

K.Scrt.D 2021. 2. 8. 10:57

HackerRank > Practice > Algorithms > Strings > Pangrams

 

문제 링크 : www.hackerrank.com/challenges/pangrams/problem

 

a-z까지의 모든 알파벳이 나타나면 'pangram'이라고 한다. (대소문자를 별도로 구분하지 않는다.)

문자가 공백일수도 있으므로 알파벳인지 확인하고, 대소문자에 따라 적절히 인덱스 값을 구하여 해당 알파벳이 사용되었음을 체크한다.

'pangram' 인 경우 'pangram'을 출력하고, 그렇지 않은 경우 'not pangram'을 출력한다.

 

#include <bits/stdc++.h>

using namespace std;

// Complete the pangrams function below.
string pangrams(string s) {
    bool chk[26] = { false };
    for (char c : s) {
        int idx;
        if (c >= 'A' && c <= 'Z') {
            idx = c - 'A';
            chk[idx] = true;
        }
        else if (c >= 'a' && c <= 'z') {
            idx = c - 'a';
            chk[idx] = true;
        }
    }
    for (int i = 0; i != 26; ++i) {
        if (!chk[i]) {
            return "not pangram";
        }
    }
    return "pangram";
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string s;
    getline(cin, s);

    string result = pangrams(s);

    fout << result << "\n";

    fout.close();

    return 0;
}

 

 

Comments