A pangram is a string that contains every letter of the English alphabet at least once, regardless of the order or frequency of the letters.
- The check is case-insensitive, so uppercase and lowercase letters are treated as the same.
- Non-alphabetic characters such as spaces, digits, and punctuation are ignored.
Examples:
Input: We promptly judged antique ivory buckles for the next prize
Output: YesInput: We promptly judged antique ivory buckles for the prize
Output: No
The first string contains all 26 letters of the English alphabet, while the second string is missing at least one letter.
Approaches to Check Whether a String is a Pangram
The following approaches can be used to check whether a string contains all 26 English alphabet letters:
1. Using a Boolean Array
This approach uses a Boolean array of size 26 to keep track of the letters present in the string.
Steps:
- Create a Boolean array of size 26 initialized to false.
- Traverse the string character by character.
- Convert uppercase letters to their corresponding lowercase index.
- Mark the corresponding position as true.
- Ignore spaces and other non-alphabetic characters.
- Check whether all 26 positions are marked true.
#include <iostream>
#include <string>
using namespace std;
bool isPangram(const string& str)
{
bool present[26] = {false};
for (char ch : str) {
if (ch >= 'A' && ch <= 'Z')
ch = ch - 'A' + 'a';
if (ch >= 'a' && ch <= 'z')
present[ch - 'a'] = true;
}
for (bool found : present) {
if (!found)
return false;
}
return true;
}
int main()
{
string str =
"We promptly judged antique ivory "
"buckles for the next prize";
cout << (isPangram(str) ? "Yes" : "No");
return 0;
}
Output
Yes
Explanation: The Boolean array stores whether each letter from a to z has appeared. For every alphabetic character, its index is calculated using ch - 'a', and that position is marked as true. If all 26 positions are marked, the string is a pangram.
2. Using STL set
This approach uses an STL set to store the distinct alphabetic characters present in the string.
Steps:
- Create an empty set of characters.
- Traverse the given string character by character.
- Convert each character to lowercase so that uppercase and lowercase letters are treated equally.
- Insert the character into the set if it is an English alphabet letter.
- Since a set stores only unique characters, duplicate letters are automatically ignored.
- After traversal, check whether the set contains all 26 English alphabet letters.
- f the set size is 26, the string is a pangram; otherwise, it is not.
#include <bits/stdc++.h>
using namespace std;
string isPangram(string s) {
// Convert each letter to lowercase
transform(s.begin(), s.end(), s.begin(), ::tolower);
// Create a frequency map of characters
unordered_map<char, int> freq;
for (char c : s) {
if (isalpha(c)) {
freq[c]++;
}
}
// Check if the frequency map contains all 26 letters
return freq.size() == 26 ? "Yes" : "No";
}
int main() {
string str = "We promptly judged antique ivory buckles for the next prize";
cout << isPangram(str) << endl;
return 0;
}
Output
Yes
Explanation: The program converts each character to lowercase and inserts alphabetic characters into a set. Since a set stores only unique characters, its size represents the number of distinct letters found. If the size is 26, every English alphabet letter is present.