Check if a string is Pangrammatic Lipogram
To understand what a pangrammatic lipogram is we will break this term down into 2 terms i.e. a pangram and a lipogram
Pangram: A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. The best-known English pangram is “The quick brown fox jumps over the lazy dog.”
Lipogram: A lipogram is a kind of constrained writing or word game consisting of writing paragraphs or longer works in which a particular letter or group of letters is avoided—usually a common vowel, and frequently E, the most common letter in the English language.
Example: The original “Mary Had a Little Lamb” was changed by A. Ross Eckler Jr. to exclude the letter ‘S’.
Original: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go He followed her to school one day That was against the rule It made the children laugh and play To see the lamb in school Lipogram (Without "S"): Mary had a little lamb With fleece a pale white hue And everywhere that Mary went The lamb kept her in view To academe he went with her, Illegal, and quite rare; It made the children laugh and play To view the lamb in there
Pangrammatic Lipogram:
A pangrammatic lipogram is a text that uses every letter of the alphabet except one. For example, “The quick brown fox jumped over the lazy dog” omits the letter S, which the usual pangram includes by using the word jumps.
Given a string, our task is to check whether this string is a pangrammatic lipogram or not?
The idea to do this is, we will keep track of all the letters which are not found in the string.
- If all the letters of the alphabet are present then its a pangram
- If only one letter is omitted then it’s a pangrammatic lipogram otherwise it can be just a lipogram.
Below is the implementation of above idea:
C++
// C++ program to check if a string // is Pangrammatic Lipogram #include<bits/stdc++.h> using namespace std; // collection of letters string alphabets = "abcdefghijklmnopqrstuvwxyz" ; // function to check for a Pangrammatic Lipogram void panLipogramChecker(string s) { // convert string to lowercase for ( int i=0; i<s.length(); i++) { s[i] = tolower (s[i]); } // variable to keep count of all the letters // not found in the string int counter = 0 ; // traverses the string for every // letter of the alphabet for ( int i=0 ; i<26 ; i++) { int pos = s.find(alphabets[i]); // if character not found in string // then increment count if (pos<0 || pos>s.length()) counter += 1; } if (counter == 0) cout<< "Pangram" <<endl; else if (counter >= 2) cout<< "Not a pangram but might a lipogram" <<endl; else cout<< "Pangrammatic Lipogram" <<endl; } // Driver program to test above function int main() { string str = "The quick brown fox jumped over the lazy dog" ; panLipogramChecker(str); str = "The quick brown fox jumps over the lazy dog" ; panLipogramChecker(str); str = "The quick brown fox jump over the lazy dog" ; panLipogramChecker(str); } |
Java
// Java program to check if a string // is Pangrammatic Lipogram import java.util.*; class GFG { // collection of letters static String alphabets = "abcdefghijklmnopqrstuvwxyz" ; /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ // function to check for a Pangrammatic Lipogram static void panLipogramChecker( char []s) { // convert string to lowercase for ( int i = 0 ; i < s.length; i++) { s[i] = Character.toLowerCase(s[i]); } // variable to keep count of all the letters // not found in the string int counter = 0 ; // traverses the string for every // letter of the alphabet for ( int i = 0 ; i < 26 ; i++) { int pos = find(s, alphabets.charAt(i)); // if character not found in string // then increment count if (pos< 0 || pos > s.length) counter += 1 ; } if (counter == 0 ) System.out.println( "Pangram" ); else if (counter >= 2 ) System.out.println( "Not a pangram but might a lipogram" ); else System.out.println( "Pangrammatic Lipogram" ); } static int find( char []arr, char c) { for ( int i = 0 ; i < arr.length; i++) { if (c == arr[i]) return 1 ; } return - 1 ; } // Driver program to test above function public static void main(String []args) { char []str = "The quick brown fox jumped over the lazy dog" .toCharArray(); panLipogramChecker(str); str = "The quick brown fox jumps over the lazy dog" .toCharArray(); panLipogramChecker(str); str = "The quick brown fox jump over the lazy dog" .toCharArray(); panLipogramChecker(str); } } // This code is contributed by Rajput-Ji |
Python3
# Python program to check if a string # is Pangrammatic Lipogram # collection of letters alphabets = 'abcdefghijklmnopqrstuvwxyz' ''' Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 ''' # function to check for a Pangrammatic Lipogram def panLipogramChecker(s): s.lower() # variable to keep count of all the letters # not found in the string counter = 0 # traverses the string for every # letter of the alphabet for ch in alphabets: # character not found in string then increment count if (s.find(ch) < 0 ): counter + = 1 if (counter = = 0 ): result = "Pangram" else if (counter = = 1 ): result = "Pangrammatic Lipogram" else : result = "Not a pangram but might a lipogram" return result # Driver program to test above function def main(): print (panLipogramChecker("The quick brown fox \ jumped over the lazy dog")) print (panLipogramChecker("The quick brown fox \ jumps over the lazy dog")) print (panLipogramChecker("The quick brown fox jump\ over the lazy dog")) if __name__ = = '__main__' : main() |
C#
// C# program to check if a string // is Pangrammatic Lipogram using System; class GFG { // collection of letters static String alphabets = "abcdefghijklmnopqrstuvwxyz" ; /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ // function to check for a Pangrammatic Lipogram static void panLipogramChecker( char []s) { // convert string to lowercase for ( int i = 0; i < s.Length; i++) { s[i] = char .ToLower(s[i]); } // variable to keep count of all the letters // not found in the string int counter = 0 ; // traverses the string for every // letter of the alphabet for ( int i = 0 ; i < 26 ; i++) { int pos = find(s, alphabets[i]); // if character not found in string // then increment count if (pos<0 || pos > s.Length) counter += 1; } if (counter == 0) Console.WriteLine( "Pangram" ); else if (counter >= 2) Console.WriteLine( "Not a pangram but might a lipogram" ); else Console.WriteLine( "Pangrammatic Lipogram" ); } static int find( char []arr, char c) { for ( int i = 0; i < arr.Length; i++) { if (c == arr[i]) return 1; } return -1; } // Driver program to test above function public static void Main(String []args) { char []str = "The quick brown fox jumped over the lazy dog" . ToCharArray(); panLipogramChecker(str); str = "The quick brown fox jumps over the lazy dog" . ToCharArray(); panLipogramChecker(str); str = "The quick brown fox jump over the lazy dog" . ToCharArray(); panLipogramChecker(str); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to check if a string // is Pangrammatic Lipogram let alphabets = "abcdefghijklmnopqrstuvwxyz" ; /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ // Function to check for a Pangrammatic Lipogram function panLipogramChecker(s) { // Convert string to lowercase for (let i = 0; i < s.length; i++) { s[i] = s[i].toLowerCase; } // Variable to keep count of all the letters // not found in the string let counter = 0; // Traverses the string for every // letter of the alphabet for (let i = 0; i < 26; i++) { let pos = s.search(alphabets[i]); // If character not found in string // then increment count if (pos < 0 || pos > s.length) counter += 1; } if (counter == 0) document.write( "Pangram" ); else if (counter >= 2) document.write( "Not a pangram but " + "might a lipogram" ); else document.write( "Pangrammatic Lipogram" ); } // Driver code let str = "The quick brown fox jumped " + "over the lazy dog" ; panLipogramChecker(str); document.write( "<br>" ); str = "The quick brown fox jumps over " + "the lazy dog" ; panLipogramChecker(str); document.write( "<br>" ); str = "The quick brown fox jump " + "over the lazy dog" ; panLipogramChecker(str); // This code is contributed by annianni </script> |
Pangrammatic Lipogram Pangram Pangrammatic Lipogram
Time Complexity: O(26 * N) , here N is the number of characters in the string to be checked and 26 represents the total number of alphabets.
Auxiliary Space: O(1)
Efficient Approach: An efficient approach will be instead of iterating through all the letters of the alphabet we can maintain a hashed array or map to store the count of occurrences of each letter of alphabet in the input string. Initially count of all the letters will be initialized to zero. We will start traversing the string and increment the count of characters. Once we have completed traversing the string then we will iterate over the map or hashed array to look for how many characters have count as zero.
Thanks to Ravi Teja Gannavarapu for suggesting this approach.
Below is implementation of above idea.
C++
// C++ program to check for a Pangrammatic // Lipogram O(n) approach /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ #include <bits/stdc++.h> using namespace std; // function to check for Pangrammatic Lipogram void panLipogramChecker(string s) { // using map to keep count of the // occurrence of each letter unordered_map< char , int > mp; for ( char c = 'a' ; c <= 'z' ; c++) { mp = 0; } transform(s.begin(), s.end(), s.begin(), :: tolower ); int i, n = s.length(); for (i = 0; i <= n - 1; i++) { if ( isalpha (s[i])) { // increment count of characters in dictionary mp[s[i]]++; } } int count_zero = 0; for ( auto it : mp) { if (it.second == 0) { count_zero++; } } if (count_zero > 1) { cout << "Not a pangram, but might be a lipogram.\n" ; } else if (count_zero == 1) { cout << "Pangrammatic Lipogram.\n" ; } else if (count_zero < 1) { cout << "Pangram.\n" ; } } // Driver program to test above function int main() { panLipogramChecker("The quick brown fox \ jumped over the lazy dog"); panLipogramChecker("The quick brown fox \ jumps over the lazy dog"); panLipogramChecker("The quick brown fox \ jump over the lazy dog"); return 0; } // This code is contributed by rajsanghavi9. |
Java
// Java program to check for a Pangrammatic // Lipogram O(n) approach /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ import java.util.*; class GFG { // function to check for Pangrammatic Lipogram static void panLipogramChecker(String s) { // using map to keep count of the // occurrence of each letter HashMap<Character, Integer> mp = new HashMap<>(); for ( char c = 'a' ; c <= 'z' ; c++) { mp.put(c, 0 ); } s = s.toLowerCase(); int i, n = s.length(); for (i = 0 ; i <= n - 1 ; i++) { if (Character.isAlphabetic(s.charAt(i))) { // increment count of characters in // dictionary mp.put(s.charAt(i), mp.get(s.charAt(i)) + 1 ); } } int count_zero = 0 ; // Getting an iterator Iterator hmIterator = mp.entrySet().iterator(); while (hmIterator.hasNext()) { Map.Entry mapElement = (Map.Entry)hmIterator.next(); int marks = (( int )mapElement.getValue()); if (marks == 0 ) count_zero++; } if (count_zero > 1 ) { System.out.println( "Not a pangram, but might be a lipogram." ); } else if (count_zero == 1 ) { System.out.println( "Pangrammatic Lipogram." ); } else if (count_zero < 1 ) { System.out.println( "Pangram." ); } } public static void main(String[] args) { panLipogramChecker( "The quick brown fox jumped over the lazy dog" ); panLipogramChecker( "The quick brown fox jumps over the lazy dog" ); panLipogramChecker( "The quick brown fox jump over the lazy dog" ); } } // This code is contributed by rajsanghavi9. |
Python3
# Python program to check for a Pangrammatic # Lipogram O(n) approach ''' Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 ''' # function to check for Pangrammatic Lipogram def panLipogramChecker(s): # dictionary to keep count of the # occurrence of each letter counter = { 'a' : 0 , 'b' : 0 , 'c' : 0 , 'd' : 0 , 'e' : 0 , 'f' : 0 , 'g' : 0 , 'h' : 0 , 'i' : 0 , 'j' : 0 , 'k' : 0 , 'l' : 0 , 'm' : 0 , 'n' : 0 , 'o' : 0 , 'p' : 0 , 'q' : 0 , 'r' : 0 , 's' : 0 , 't' : 0 , 'u' : 0 , 'v' : 0 , 'w' : 0 , 'x' : 0 , 'y' : 0 , 'z' : 0 } s = s.lower() # increment count of characters in dictionary for i in s: if (i.isalpha()): counter[i] + = 1 # returns a list containing the values of all # the keys in h=the dictionary b = list (counter.values()) if (b.count( 0 ) > 1 ): print ( "Not a pangram, but might be a lipogram." ) else if (b.count( 0 ) = = 1 ): print ( "Pangrammatic Lipogram." ) else if (b.count( 0 ) < 1 ): print ( "Pangram." ) # Driver program to test above function def main(): panLipogramChecker("The quick brown fox \ jumped over the lazy dog") panLipogramChecker("The quick brown fox \ jumps over the lazy dog") panLipogramChecker("The quick brown fox \ jump over the lazy dog") if __name__ = = '__main__' : main() |
C#
// C# program to check for a Pangrammatic // Lipogram O(n) approach /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ using System; using System.Collections.Generic; class GFG { // function to check for a Pangrammatic Lipogram static void panLipogramChecker( string s) { // using map to keep count of the // occurrence of each letter Dictionary< char , int > mp = new Dictionary< char , int >(); for ( char c = 'a' ; c <= 'z' ; c++) { mp.Add(c, 0); } s = s.ToLower(); int i, n = s.Length; for (i = 0; i <= n - 1; i++) { if (Char.IsLetter(s[i])) { // increment count of characters in // dictionary mp[s[i]] = mp[s[i]] + 1; } } int count_zero = 0, marks; // Getting an iterator foreach (KeyValuePair< char , int > entry in mp) { marks = ( int ) entry.Value; if (marks == 0) count_zero++; } if (count_zero > 1) { Console.WriteLine( "Not a pangram, but might be a lipogram." ); } else if (count_zero == 1) { Console.WriteLine( "Pangrammatic Lipogram." ); } else if (count_zero < 1) { Console.WriteLine( "Pangram." ); } } // Driver program to test above function public static void Main(String []args) { string str = "The quick brown fox jumped over the lazy dog" ; panLipogramChecker(str); str = "The quick brown fox jumps over the lazy dog" ; panLipogramChecker(str); str = "The quick brown fox jump over the lazy dog" ; panLipogramChecker(str); } } // This code is contributed by kothavvsaakash |
Javascript
<script> // JavaScript program to check for a Pangrammatic // Lipogram O(n) approach /* Category No of letters unmatched Pangram 0 Lipogram >1 Pangrammatic Lipogram 1 */ // count number of occurrences of `n` in arr const count = (arr , n) => arr.filter(x => x === n).length // function to check for Pangrammatic Lipogram const panLipogramChecker = (s) => { const counter = { 'a' : 0, 'b' : 0, 'c' : 0, 'd' : 0, 'e' : 0, 'f' : 0, 'g' : 0, 'h' : 0, 'i' : 0, 'j' : 0, 'k' : 0, 'l' : 0, 'm' : 0, 'n' : 0, 'o' : 0, 'p' : 0, 'q' : 0, 'r' : 0, 's' : 0, 't' : 0, 'u' : 0, 'v' : 0, 'w' : 0, 'x' : 0, 'y' : 0, 'z' : 0} s = s.toLowerCase() // Couting frequency of each character for (let i of s) { if (/^[a-z]+$/gi.test(i)) { counter[i] += 1 } } const b = Object.values(counter) if (count(b , 0) > 1) { document.write( "Not a pangram, but might be a lipogram." ) } else if (count(b , 0) == 1) { document.write( "Pangrammatic Lipogram." ) } else { document.write( "Pangram." ) } } // Driver code panLipogramChecker( "The quick brown fox jumped over the lazy dog" ) panLipogramChecker( "The quick brown fox jumps over the lazy dog" ) panLipogramChecker( "The quick brown fox jump over the lazy dog" ) // This code is contributed by kraanzu. </script> |
Pangrammatic Lipogram. Pangram. Not a pangram, but might be a lipogram.
Time Complexity: O(N), where N is the number of characters in the input string.
Auxiliary Space: O(N), due to map
This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...