We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here it is, essentially in C (especially in the char-to-index conversion), O(n) in the length of the input string, and somewhat optimized to avoid extra execution:
bool hasIt[26] = { false, false, false, false, false,
false, false, false, false, false,
false, false, false, false, false,
false, false, false, false, false,
false, false, false, false, false,
false }; // Or use a loop, or something modern
int uniqueFound = 0; // How many distinct letters we have found
string s;
getline(cin, s);
for (string::iterator c = s.begin(); c != s.end(); c++) // <- This is C++
{
// Assume 'a' to 'z' is contiguous and 'A' to 'Z' is contiguous
int idx = -1;
if ((*c >= 'a') && (*c <= 'z'))
idx = *c - 'a';
else if ((*c >= 'A') && (*c <= 'Z'))
idx = *c - 'A';
else
continue;
// We have an alphabetic and have found its index in the alphabet
if (!hasIt[idx])
uniqueFound++;
if (uniqueFound >= 26)
break;
hasIt[idx] = true;
}
// We either found every letter, or we exhausted the input string
if (uniqueFound < 26)
cout << "not ";
cout << "pangram\n";
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pangrams
You are viewing a single comment's thread. Return to all comments →
Here it is, essentially in C (especially in the char-to-index conversion), O(n) in the length of the input string, and somewhat optimized to avoid extra execution: