# Check if given String is Pangram or not

Given a string Str. The task is to check if it is Pangram or not.

A pangram is a sentence containing every letter in the English Alphabet.

Examples:

Input: “The quick brown fox jumps over the lazy dog”
Output: is a Pangram
Explanation: Contains all the characters from â€˜aâ€™ to â€˜zâ€™]

Input: “The quick brown fox jumps over the dog”
Output: is not a Pangram
Explanation: Doesnâ€™t contain all the characters from â€˜aâ€™ to â€˜zâ€™, as â€˜lâ€™, â€˜zâ€™, â€˜yâ€™ are missing

Recommended Practice

Approach 1: Below is the idea to solve the problem

Create a mark[] array of Boolean types and iterate through all the characters of the string and mark it as visited. Lowercase and Uppercase are considered the same. So â€˜Aâ€™ and â€˜aâ€™ are marked in index 0 and similarly â€˜Zâ€™ and â€˜zâ€™ are marked in index 25.

After iterating through all the characters check whether all the characters are marked or not. If not then return false as this is not a pangram else return true

Follow the below steps to Implement the idea:

• Create a bool vector mark[] of size 26.
• Iterate through all characters of the string str and mark str[i] – ‘a’ or str[i] – ‘A’ as 1 for lower and upper characters respectively.
• Iterate through all the indices of mark[]
• If all indices are marked visited then return is a Pangram
• Else return  is not a Pangram.

Below is the Implementation of above approach

## C++

 `// A C++ Program to check if the given``// string is a pangram or not``#include ``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 other than english``        ``// lowercase and uppercase characters.``        ``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 Program to test above functions``int` `main()``{``    ``string str = ``"The quick brown fox jumps over the"``                 ``" lazy dog"``;``    ``if` `(checkPangram(str) == ``true``)``        ``printf``(``" %s \nis a pangram"``, str.c_str());``    ``else``        ``printf``(``" %s \nis not a pangram"``, str.c_str());``    ``return` `(0);``}` `// This code is contributed by Aditya kumar (adityakumar129)`

## C

 `// A C Program to check if the given``// string is a pangram or not``#include ``#include ``#include ` `// Returns true if the string is pangram else false``bool` `checkPangram(``char` `str[])``{``    ``// Create a hash table to mark the characters``    ``// present in the string``    ``bool` `mark[26];``    ``for` `(``int` `i = 0; i < 26; i++)``        ``mark[i] = ``false``;` `    ``// For indexing in mark[]``    ``int` `index;` `    ``// Traverse all characters``    ``size_t` `size = ``strlen``(str);``    ``for` `(``int` `i = 0; i < size; 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 other than english``        ``// lowercase and uppercase characters.``        ``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 Program to test above functions``int` `main()``{``    ``char` `str[]``        ``= ``"The quick brown fox jumps over the lazy dog"``;``    ``if` `(checkPangram(str) == ``true``)``        ``printf``(``" %s \nis a pangram"``, str);``    ``else``        ``printf``(``" %s \nis not a pangram"``, str);``    ``return` `(0);``}` `// This code is contributed by Aditya kumar (adityakumar129)`

## Java

 `// Java Program to illustrate Pangram` `import` `java.io.*;` `class` `GFG {` `    ``// Returns true if the string``    ``// is pangram else false``    ``public` `static` `boolean` `checkPangram(String str)``    ``{``        ``// Create a hash table to mark the``        ``// characters present in the string``        ``// By default all the elements of``        ``// mark would be false.``        ``boolean``[] mark = ``new` `boolean``[``26``];` `        ``// For indexing in mark[]``        ``int` `index = ``0``;` `        ``// Traverse all characters``        ``for` `(``int` `i = ``0``; i < str.length(); i++) {``            ``// If uppercase character, subtract 'A'``            ``// to find index.``            ``if` `(``'A'` `<= str.charAt(i)``                ``&& str.charAt(i) <= ``'Z'``)``                ``index = str.charAt(i) - ``'A'``;` `            ``// If lowercase character, subtract 'a'``            ``// to find index.``            ``else` `if` `(``'a'` `<= str.charAt(i)``                     ``&& str.charAt(i) <= ``'z'``)` `                ``index = str.charAt(i) - ``'a'``;` `            ``// If this character is other than english``            ``// lowercase and uppercase characters.``            ``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``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String str``            ``= ``"The quick brown fox jumps over the lazy dog"``;` `        ``if` `(checkPangram(str) == ``true``)``            ``System.out.print(str + ``" \nis a pangram."``);``        ``else``            ``System.out.print(str + ``" \nis not a pangram."``);``    ``}``}`

## Python3

 `# A Python Program to check if the given``# string is a pangram or not`  `def` `checkPangram(s):``    ``List` `=` `[]``    ``# create list of 26 characters and set false each entry``    ``for` `i ``in` `range``(``26``):``        ``List``.append(``False``)` `    ``# converting the sentence to lowercase and iterating``    ``# over the sentence``    ``for` `c ``in` `s.lower():``        ``if` `not` `c ``=``=` `" "``:` `            ``# make the corresponding entry True``            ``List``[``ord``(c) ``-` `ord``(``'a'``)] ``=` `True` `    ``# check if any character is missing then return False``    ``for` `ch ``in` `List``:``        ``if` `ch ``=``=` `False``:``            ``return` `False``    ``return` `True`  `# Driver Program to test above functions``sentence ``=` `"The quick brown fox jumps over the little lazy dog"` `if` `(checkPangram(sentence)):``    ``print``(``'"'+sentence+'"'``)``    ``print``(``"\nis a pangram"``)``else``:``    ``print``(``'"'+sentence+'"'``)``    ``print``(``"\nis not a pangram"``)` `# This code is contributed by Danish Mushtaq`

## C#

 `// C# Program to illustrate Pangram``using` `System;``class` `GFG {` `    ``// Returns true if the string``    ``// is pangram else false``    ``public` `static` `bool` `checkPangram(``string` `str)``    ``{` `        ``// Create a hash table to mark the``        ``// characters present in the string``        ``// By default all the elements of``        ``// mark would be false.``        ``bool``[] mark = ``new` `bool``[26];` `        ``// For indexing in mark[]``        ``int` `index = 0;` `        ``// 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 other than english``            ``// lowercase and uppercase characters.``            ``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``    ``public` `static` `void` `Main()``    ``{``        ``string` `str``            ``= ``"The quick brown fox jumps over the lazy dog"``;` `        ``if` `(checkPangram(str) == ``true``)``            ``Console.WriteLine(str + ``" \nis a pangram."``);``        ``else``            ``Console.WriteLine(str + ``" \nis not a pangram."``);``    ``}``}` `// This code is contributed by nitin mittal.`

## Javascript

 ``

Output
``` The quick brown fox jumps over the lazy dog
is a pangram```

Time Complexity : O(n), where n is the length of our string
Auxiliary Space: O(1), as 26 size Boolean vector is constant.

Approach 2 (Using Set) : The idea to use a set is quite obvious because to check a pangram, there must be 26 alphabets no matter whether it is a lowercase or uppercase character. To check whether a alphabet is already present or not, the alphabets have been inserted in lowercase and the set size must be 26.

Follow the below steps to Implement the idea :

• Initialize a character set.
• Traverse over the string and check for each character.
• If the character is an lowercase alphabet then insert in the set.
• In case it is a uppercase alphabet convert it in lowercase using the tolower() function and then insert it.
• At the end simply check whether the set size is 26 or not. If it’s true that means all alphabets in either lowercase or uppercase was present in the string.

Below is the code for the following approach :

## C++

 `// A C++ Program to check if the given``// string is a pangram or not``#include ``using` `namespace` `std;` `// Returns true if the string is pangram else false``bool` `checkPangram(string& str)``{``    ``// Initialize a set of characters``    ``set<``char``> set;` `    ``for` `(``auto` `ch : str) {``        ``// If the character is already``        ``// a lowercase character``        ``if` `(ch >= ``'a'` `and ch <= ``'z'``)``            ``set.insert(ch);` `        ``// In case of a uppercase character``        ``if` `(ch >= ``'A'` `and ch <= ``'Z'``) {``            ``// convert to lowercase``            ``ch = ``tolower``(ch);``            ``set.insert(ch);``        ``}``    ``}` `    ``// check if the size is 26 or not``    ``return` `set.size() == 26;``}` `// Driver Program to test above functions``int` `main()``{``    ``string str = ``"The quick brown fox jumps over the"``                 ``" lazy dog"``;``    ``if` `(checkPangram(str) == ``true``)``        ``cout << ``"It is a Pangram"` `<< endl;``    ``else``        ``cout << ``"It is Not a Pangram"` `<< endl;` `    ``return` `0;``}` `// This code is contributed by Rajdeep Mallick``// (rajdeepmallick999)`

## Java

 `// A Java Program to check if the given``// string is a pangram or not``import` `java.util.*;` `public` `class` `Gfg {` `    ``// Returns true if the string is pangram else false``    ``static` `boolean` `checkPangram(String str) {``        ``// Initialize a set of characters``        ``Set set = ``new` `HashSet<>();` `        ``for` `(``char` `ch : str.toCharArray()) {``            ``// If the character is already``            ``// a lowercase character``            ``if` `(ch >= ``'a'` `&& ch <= ``'z'``)``                ``set.add(ch);` `            ``// In case of a uppercase character``            ``if` `(ch >= ``'A'` `&& ch <= ``'Z'``) {``                ``// convert to lowercase``                ``ch = Character.toLowerCase(ch);``                ``set.add(ch);``            ``}``        ``}` `        ``// check if the size is 26 or not``        ``return` `set.size() == ``26``;``    ``}` `    ``// Driver Program to test above functions``    ``public` `static` `void` `main(String[] args) {``        ``String str = ``"The quick brown fox jumps over the lazy dog"``;``        ``if` `(checkPangram(str))``            ``System.out.println(``"It is a Pangram"``);``        ``else``            ``System.out.println(``"It is Not a Pangram"``);``    ``}``}`

## Python3

 `#A Python3 Program to check if the given``#string is a pangram or not``#Returns true if the string is pangram else false``def` `checkPangram(string):``    ``# Initialize a set of characters``    ``char_set ``=` `set``()``    ` `    ``for` `ch ``in` `string:``        ``# If the character is already``        ``# a lowercase character``        ``if` `ch >``=` `'a'` `and` `ch <``=` `'z'``:``            ``char_set.add(ch)``    ` `        ``# In case of a uppercase character``        ``if` `ch >``=` `'A'` `and` `ch <``=` `'Z'``:``            ``# convert to lowercase``            ``ch ``=` `ch.lower()``            ``char_set.add(ch)` `    ``# check if the size is 26 or not``    ``return` `len``(char_set) ``=``=` `26` `#Driver Program to test above functions``string ``=` `"The quick brown fox jumps over the lazy dog"``if` `checkPangram(string) ``=``=` `True``:``    ``print``(``"It is a Pangram"``)``else``:``    ``print``(``"It is Not a Pangram"``)`

## C#

 `using` `System;``using` `System.Collections.Generic;` `class` `Program``{``    ``static` `bool` `CheckPangram(``string` `str)``    ``{``        ``// Initialize a set of characters``        ``HashSet<``char``> ``set` `= ``new` `HashSet<``char``>();` `        ``foreach` `(``char` `ch ``in` `str)``        ``{``            ``// If the character is already a lowercase character``            ``if` `(ch >= ``'a'` `&& ch <= ``'z'``)``                ``set``.Add(ch);` `            ``// In case of an uppercase character``            ``if` `(ch >= ``'A'` `&& ch <= ``'Z'``)``            ``{``                ``// convert to lowercase``                ``char` `lowerCh = Char.ToLower(ch);``                ``set``.Add(lowerCh);``            ``}``        ``}` `        ``// check if the size is 26 or not``        ``return` `set``.Count == 26;``    ``}` `    ``static` `void` `Main()``    ``{``        ``string` `str = ``"The quick brown fox jumps over the lazy dog"``;``        ``if` `(CheckPangram(str))``            ``Console.WriteLine(``"It is a Pangram"``);``        ``else``            ``Console.WriteLine(``"It is Not a Pangram"``);``    ``}``}`

## Javascript

 `// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)``// Returns true if the string is pangram else false``function` `checkPangram(str) {``    ``// Initialize a set of characters``    ``const set = ``new` `Set();` `    ``for` `(let ch of str) {``        ``// If the character is already``        ``// a lowercase character``        ``if` `(ch >= ``'a'` `&& ch <= ``'z'``) {``            ``set.add(ch);``        ``}` `        ``// In case of an uppercase character``        ``if` `(ch >= ``'A'` `&& ch <= ``'Z'``) {``            ``// convert to lowercase``            ``ch = ch.toLowerCase();``            ``set.add(ch);``        ``}``    ``}` `    ``// check if the size is 26 or not``    ``return` `set.size === 26;``}` `// Driver Program to test above functions``const str = ``"The quick brown fox jumps over the lazy dog"``;``if` `(checkPangram(str)) {``    ``console.log(``"It is a Pangram"``);``} ``else` `{``    ``console.log(``"It is Not a Pangram"``);``}`

Output
`It is a Pangram`

Time Complexity : O(NlogN), for traversing whole string and inserting all the characters at worst case takes logN time.
Space Complexity : O(1), using constant space for 26 characters at most.

Previous
Next