Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a string is Pangrammatic Lipogram in Python
A Pangram is a string where every letter of the alphabet appears at least once. A Lipogram is a string where one or more letters are missing. A Pangrammatic Lipogram is a special case where exactly one letter is missing from the alphabet.
In this tutorial, we'll learn how to identify these types of strings in Python by counting missing letters.
Algorithm
To solve this problem, we need to ?
- Convert the string to lowercase for case-insensitive comparison
- Count how many alphabet letters are missing from the string
- Classify based on the count:
- 0 missing ? Pangram
- 1 missing ? Pangrammatic Lipogram
- 2+ missing ? Lipogram
Implementation
import string
def solve(input_string):
input_string = input_string.lower()
missing_count = 0
for character in string.ascii_lowercase:
if character not in input_string:
missing_count += 1
if missing_count == 0:
return "The String is a Pangram"
elif missing_count == 1:
return "The String is a Pangrammatic Lipogram"
else:
return "The String isn't a Pangram but might be a Lipogram"
# Test with different strings
test_strings = [
"pack my box with five dozen liquor jugs",
"to stay in this mortal world or by my own hand go to oblivion, that is my conundrum.",
"the quick brown fox jumps over a lazy dog",
"waltz, nymph, for quick jigs ve bud"
]
for test_string in test_strings:
print(solve(test_string))
The String is a Pangram The String isn't a Pangram but might be a Lipogram The String is a Pangram The String is a Pangrammatic Lipogram
How It Works
The function works by iterating through all 26 lowercase letters and checking if each one exists in the input string. Here's the step-by-step process ?
-
Normalize input: Convert to lowercase using
lower() -
Count missing letters: Use
string.ascii_lowercaseto check each letter a-z - Classify result: Based on how many letters are missing
Alternative Approach Using Sets
We can also solve this using set operations for better performance ?
import string
def solve_with_sets(input_string):
alphabet_set = set(string.ascii_lowercase)
string_letters = set(input_string.lower())
missing_letters = alphabet_set - string_letters
missing_count = len(missing_letters)
if missing_count == 0:
return "The String is a Pangram"
elif missing_count == 1:
return "The String is a Pangrammatic Lipogram"
else:
return "The String isn't a Pangram but might be a Lipogram"
# Test the set-based approach
print(solve_with_sets("the quick brown fox jumps over a lazy dog"))
print(solve_with_sets("waltz, nymph, for quick jigs ve bud"))
The String is a Pangram The String is a Pangrammatic Lipogram
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Character iteration | O(26 × n) | O(1) | Small strings |
| Set operations | O(n) | O(n) | Large strings |
Conclusion
Use character iteration for simple cases or set operations for better performance with large strings. Both methods effectively classify strings as Pangrams, Lipograms, or Pangrammatic Lipograms by counting missing alphabet letters.
