Open In App
Related Articles

C++ program to check whether a String is a Pangram or not

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given string str, the task is to check whether a string is pangram or not using in C++.

A string is a Pangram if the string contains all the English alphabet letters.


Examples: 

Input: str = “We promptly judged antique ivory buckles for the next prize” 
Output: Yes 
Explanations: In the above string, str has all the English alphabet letters.

Input: str = “We promptly judged antique ivory buckles for the prize” 
Output: No 

 

Method-1:  Without using STL

This approach is based on Hashing. 

  1. Create a boolean hash table of size 26, one for each letter of the English alphabet.
  2.  Traverse the given string character by character.
  3.  For each character in the string, calculate its corresponding index in the hash table using the ASCII value. For example, the ASCII value of ‘a’ is 97, so its index in the hash table would be 97 – 97 = 0. Similarly, the index of ‘b’ would be 98 – 97 = 1, and so on.
  4.  Mark the index in the hash table as true, indicating that the corresponding letter is present in the string.
  5.  After complete traversal and marking of the string, traverse the hash table and check if all the indices have been marked as true or not. If all the indices have been marked as true, then the string contains all the letters of the English alphabet, and the algorithm returns true. Otherwise, the string does not contain all the letters of the English alphabet, and the algorithm returns false.

The time complexity of this algorithm is O(n), where n is the length of the string, since we are traversing the string only once, and the hash table operations take constant time. The space complexity is also O(1) since the size of the hash table is fixed and does not depend on the length of the input string.

Note that this approach can be easily extended to check if a given string contains all the letters of any other language or character set, by creating a hash table of appropriate size and mapping the characters to their corresponding indices.

Below is the implementation of the above approach:

C++

// C++ Program to check if the given
// string is a pangram or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if the string is
// pangram else false
bool checkPangram(string& str)
{
    // Create a hash table to mark
    // the characters
    // present in the string
    vector<bool> mark(26, false);
 
    // For indexing in mark[]
    int index;
 
    // Traverse all characters
    for (int i = 0; i < str.length(); i++) {
 
        // If uppercase character,
        // subtract 'A' to find index.
        if ('A' <= str[i] && str[i] <= 'Z')
            index = str[i] - 'A';
 
        // If lowercase character,
        // subtract 'a' to find index.
        else if ('a' <= str[i]
                 && str[i] <= 'z')
            index = str[i] - 'a';
 
        // If this character is not
        // an alphabet, skip to next one.
        else
            continue;
 
        mark[index] = true;
    }
 
    // Return false
    // if any character is unmarked
    for (int i = 0; i <= 25; i++)
        if (mark[i] == false)
            return (false);
 
    // If all characters were present
    return (true);
}
 
// Driver Code
int main()
{
    string str = "We promptly judged"
                 " antique ivory"
                 " buckles for the next prize";
 
    if (checkPangram(str) == true)
        printf("Yes");
    else
        printf("No");
 
    return (0);
}

                    

Output
Yes

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)

Method-2: Using STL

The transform() method of STL can be used to check whether the given string is Pangram or not.

Syntax:

transform(s.begin(), s.end(), s.begin(), ::toupper);

Approach:
In order to check if the string contains all the alphabets of the English alphabet: 

Step 1: Convert all the letters in the string to either uppercase or lowercase using the transform() method of the STL library. This is done to avoid treating lowercase and uppercase letters as different entities while checking for the presence of all 26 letters of the English alphabet.
Step 2: Sort the string using the sort() method of the STL library. This step is required to identify the distinct letters present in the string.
Step 3:  Use the unique() method of the STL library to identify the unique letters present in the sorted string. The unique() method rearranges the string such that all the unique letters come first and returns an iterator to the position of the first non-unique letter.
Step 4: Count the number of unique letters present in the string using the distance() method of the STL library, which calculates the distance between two iterators.

Step 5: Check if the count of unique letters is equal to 26 (since there are 26 letters in the English alphabet) or not. If the count is 26, then the given string is a pangram, and the algorithm returns true. Otherwise, the given string is not a pangram, and the algorithm returns false.

Note that space is also considered as a distinct entity, which is why the count is checked against 27 instead of 26.

The time complexity of this algorithm is O(n log n), where n is the length of the input string, since we are sorting the string using the sort() method. The space complexity is O(n), since we are creating a new string to store the converted and sorted version of the input string.

Below is the implementation of the above approach:

CPP

// C++ Program to check whether
// a string pangram or not using STL
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return given string
// str is pangrams yes or no
string pangrams(string s)
{
 
    // Initialization of count
    int count = 0;
 
    // Convert each letter into
    // uppercase to avoid counting
    // of both uppercase and
    // lowercase as different letters
    transform(s.begin(),
              s.end(),
              s.begin(),
              ::toupper);
 
    // Sort the string
    sort(s.begin(), s.end());
 
    // Count distinct alphabets
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != s[i + 1])
            count++;
    }
   
    // If count is 27 then the string
    // contains all the alphabets
    // including space as a
    // distinct character
    if (count == 27)
        return "Yes";
 
    else
        return "No";
}
 
// Driver code
int main()
{
    // Given string str
    string str = "We promptly "
                 "judged antique"
                 "ivory buckles for "
                 "the next prize";
 
    // Function Call
    cout << pangrams(str);
 
    return 0;
}

                    

C++

#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++;
        }
    }
 
    // 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

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)



Last Updated : 17 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads