C++ program to check whether a String is a Pangram or not
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.
- Create a boolean hash table of size 26, one for each letter of the English alphabet.
- Traverse the given string character by character.
- 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.
- Mark the index in the hash table as true, indicating that the corresponding letter is present in the string.
- 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++
#include <bits/stdc++.h>
using namespace std;
bool checkPangram(string& str)
{
vector< bool > mark(26, false );
int index;
for ( int i = 0; i < str.length(); i++) {
if ( 'A' <= str[i] && str[i] <= 'Z' )
index = str[i] - 'A' ;
else if ( 'a' <= str[i]
&& str[i] <= 'z' )
index = str[i] - 'a' ;
else
continue ;
mark[index] = true ;
}
for ( int i = 0; i <= 25; i++)
if (mark[i] == false )
return ( false );
return ( true );
}
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);
}
|
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
#include <bits/stdc++.h>
using namespace std;
string pangrams(string s)
{
int count = 0;
transform(s.begin(),
s.end(),
s.begin(),
:: toupper );
sort(s.begin(), s.end());
for ( int i = 0; i < s.size(); i++) {
if (s[i] != s[i + 1])
count++;
}
if (count == 27)
return "Yes" ;
else
return "No" ;
}
int main()
{
string str = "We promptly "
"judged antique"
"ivory buckles for "
"the next prize" ;
cout << pangrams(str);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
string isPangram(string s) {
transform(s.begin(), s.end(), s.begin(), :: tolower );
unordered_map< char , int > freq;
for ( char c : s) {
if ( isalpha (c)) {
freq++;
}
}
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;
}
|
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
Share your thoughts in the comments
Please Login to comment...