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++
// 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>
OutputPangrammatic 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[c] = 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()
// Counting 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>
OutputPangrammatic 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
Similar Reads
Check if a string is Isogram or not
Given a word or phrase, check if it is an isogram or not. An Isogram is a word in which no letter occurs more than once Examples: Input: MachineOutput: TrueExplanation: "Machine" does not have any character repeating, it is an Isogram Input : GeekOutput : FalseExplanation: "Geek" has 'e' as repeatin
9 min read
Check if given String is Pangram or not
Given a string s, the task is to check if it is Pangram or not. A pangram is a sentence containing all letters of the English Alphabet.Examples: Input: s = "The quick brown fox jumps over the lazy dog" Output: trueExplanation: The input string contains all characters from âaâ to âzâ.Input: s = "The
6 min read
Python set to check if string is pangram
Given a string, check if the given string is a pangram or not. Examples: Input : The quick brown fox jumps over the lazy dog Output : The string is a pangram Input : geeks for geeks Output : The string is not pangram A normal way would have been to use frequency table and check if all elements were
2 min read
Check if a given string is a rotation of a palindrome
Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p
15+ min read
Check if all given strings are isograms or not
Given an array arr containing N strings, the task is to check if all strings are isogram or not. If they are, print Yes, otherwise No. An Isogram is a word in which no letter occurs more than once. Examples: Input: arr[] = {"abcd", "derg", "erty"}Output: Yes Input: arr[] = {"agka", "lkmn"}Output: No
4 min read
Check given string is oddly palindrome or not | Set 2
Given string str, the task is to check if characters at the odd indexes of str form a palindrome string or not. If not then print "No" else print "Yes". Examples: Input: str = "osafdfgsg", N = 9 Output: Yes Explanation: Odd indexed characters are = { s, f, f, s } so it will make palindromic string,
11 min read
Check given string is oddly palindrome or not
Given string str, the task is to check if characters at the odd indexes of str form a palindrome string or not. If not then print "No" else print "Yes".Examples: Input: str = "osafdfgsg", N = 9 Output: Yes Explanation: Odd indexed characters are = { s, f, f, s } so it will make palindromic string, "
6 min read
Check if a linked list of strings forms a palindrome
Given a linked list handling string data, check to see whether data is palindrome or not? For example, Input : a -> bc -> d -> dcb -> a -> NULL Output : True String "abcddcba" is palindrome. Output : a -> bc -> d -> ba -> NULL Output : False String "abcdba" is not palindro
6 min read
Missing characters to make a string Pangram
Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order. Examples: Input : welcome to geeksforgeeksOutput : abdhij
8 min read
Check if a number is a Pangram or not
Given an integer N, the task is to check whether the given number is a pangram or not. Note: A Pangram Number contains every digit [0- 9] at least once. Examples: Input : N = 10239876540022Output : YesExplanation: N contains all the digits from 0 to 9. Therefore, it is a pangram. Input : N = 2345678
7 min read