Check if a string is Pangrammatic Lipogram
Last Updated :
20 Apr, 2023
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++
#include<bits/stdc++.h>
using namespace std;
string alphabets = "abcdefghijklmnopqrstuvwxyz" ;
void panLipogramChecker(string s)
{
for ( int i=0; i<s.length(); i++)
{
s[i] = tolower (s[i]);
}
int counter = 0 ;
for ( int i=0 ; i<26 ; i++)
{
int pos = s.find(alphabets[i]);
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;
}
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
import java.util.*;
class GFG
{
static String alphabets = "abcdefghijklmnopqrstuvwxyz" ;
static void panLipogramChecker( char []s)
{
for ( int i = 0 ; i < s.length; i++)
{
s[i] = Character.toLowerCase(s[i]);
}
int counter = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
int pos = find(s, alphabets.charAt(i));
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 ;
}
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);
}
}
|
Python3
alphabets = 'abcdefghijklmnopqrstuvwxyz'
def panLipogramChecker(s):
s.lower()
counter = 0
for ch in alphabets:
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
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#
using System;
class GFG
{
static String alphabets = "abcdefghijklmnopqrstuvwxyz" ;
static void panLipogramChecker( char []s)
{
for ( int i = 0; i < s.Length; i++)
{
s[i] = char .ToLower(s[i]);
}
int counter = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
int pos = find(s, alphabets[i]);
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;
}
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);
}
}
|
Javascript
<script>
let alphabets = "abcdefghijklmnopqrstuvwxyz" ;
function panLipogramChecker(s)
{
for (let i = 0; i < s.length; i++)
{
s[i] = s[i].toLowerCase;
}
let counter = 0;
for (let i = 0; i < 26; i++)
{
let pos = s.search(alphabets[i]);
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" );
}
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);
</script>
|
Output
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++
#include <bits/stdc++.h>
using namespace std;
void panLipogramChecker(string s)
{
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])) {
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" ;
}
}
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;
}
|
Java
import java.util.*;
class GFG {
static void panLipogramChecker(String s)
{
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))) {
mp.put(s.charAt(i),
mp.get(s.charAt(i)) + 1 );
}
}
int count_zero = 0 ;
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" );
}
}
|
Python3
def panLipogramChecker(s):
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()
for i in s:
if (i.isalpha()):
counter[i] + = 1
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." )
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#
using System;
using System.Collections.Generic;
class GFG
{
static void panLipogramChecker( string s)
{
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]))
{
mp[s[i]] = mp[s[i]] + 1;
}
}
int count_zero = 0, marks;
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." );
}
}
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);
}
}
|
Javascript
<script>
const count = (arr , n) => arr.filter(x => x === n).length
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()
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." )
}
}
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" )
</script>
|
Output
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
Share your thoughts in the comments
Please Login to comment...