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 Isogram or not in Python
An isogram is a string where each letter appears exactly once. In Python, we can check if a string is an isogram using various approaches like sets, lists, or built-in functions.
For example, "education" is an isogram because each letter (e, d, u, c, a, t, i, o, n) appears only once.
Method 1: Using a List to Track Characters
We can iterate through each character and maintain a list of seen characters ?
def check_isogram_with_list(word):
char_list = []
for char in word:
if char.isalpha():
if char.lower() in char_list:
return False
char_list.append(char.lower())
return True
# Test the function
s = "education"
print(f"'{s}' is isogram:", check_isogram_with_list(s))
s = "hello"
print(f"'{s}' is isogram:", check_isogram_with_list(s))
'education' is isogram: True 'hello' is isogram: False
Method 2: Using a Set for Efficient Lookup
Sets provide O(1) lookup time, making this approach more efficient ?
def check_isogram_with_set(word):
seen_chars = set()
for char in word:
if char.isalpha():
char_lower = char.lower()
if char_lower in seen_chars:
return False
seen_chars.add(char_lower)
return True
# Test the function
test_words = ["education", "programming", "Python", "123abc"]
for word in test_words:
result = check_isogram_with_set(word)
print(f"'{word}' is isogram: {result}")
'education' is isogram: True 'programming' is isogram: False 'Python' is isogram: True '123abc' is isogram: True
Method 3: Using Set Length Comparison
Compare the length of original alphabetic characters with unique characters ?
def check_isogram_set_length(word):
# Extract only alphabetic characters and convert to lowercase
alpha_chars = [char.lower() for char in word if char.isalpha()]
# Compare length of list with set (unique characters)
return len(alpha_chars) == len(set(alpha_chars))
# Test with various strings
test_cases = ["education", "Hello", "Programming", "abcdef", ""]
for test in test_cases:
result = check_isogram_set_length(test)
print(f"'{test}' is isogram: {result}")
'education' is isogram: True 'Hello' is isogram: False 'Programming' is isogram: False 'abcdef' is isogram: True '' is isogram: True
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| List tracking | O(n²) | O(n) | Educational purposes |
| Set lookup | O(n) | O(n) | Large strings |
| Set length comparison | O(n) | O(n) | Concise solution |
Key Points
- Use
char.lower()to handle case-insensitive comparison - Use
char.isalpha()to ignore non-alphabetic characters - Set-based approaches are more efficient for larger strings
- Empty strings are considered isograms by definition
Conclusion
Use the set length comparison method for the most concise and efficient solution. The set lookup method offers good performance with clear logic, while the list method is useful for understanding the concept step by step.
