AI Forever
3 subscribers
1 link
🌱 Beginner → � Intermediate → 🚀 Advanced → 🏆 Mastery
Download Telegram
🚀 Problem 2

Problem: Determine if a string is an isogram (no repeating letters, case-insensitive).
Examples:
- "Dermatoglyphics" → True
- "moOse" → False (duplicate 'o' when lowercased)
- Empty string → True

🧑💻 My Approach:
def is_isogram(string):
string = string.lower()
for i in string:
if string.count(i) > 1:
return False
return True

Works but…
- Checks each character's count, leading to O(n²) time complexity.
- Early exit if duplicates found, but inefficient for large strings.

🤖 AI's Optimized Approach:
def is_isogram(string):
lowered = string.lower()
return len(set(lowered)) == len(lowered)

🔥 Why Better?
1. O(n) Time: Converts string to a set (auto-removes duplicates).
2. Concise: One-liner using Python's built-in functions.
3. Readable: Leverages set properties for uniqueness check.

💡 Key Takeaways:
- Use Sets for Uniqueness: Perfect for checking duplicates.
- Case Handling: Always lowercase the string first.
- Edge Cases: Empty string is handled gracefully.

🔍 Compare Efficiency:
| Method | Time Complexity | Lines of Code |
|--------------|-----------------|---------------|
| Loop & Count | O(n²) | 5 |
| Set & Length | O(n) | 1 |

*Credits: Codewars problem + ChatGPT optimization.*

Try both methods and share your thoughts! 👇

#Python #CodingTips #Algorithms #Programming