Commit 1a164c7
authored
perf: optimize is_pangram by fixing unnecessary list creation
Issue: Current implementation creates an intermediate list from a set, which is redundant and inefficient.
**Before:**
```python
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) == 26
```
**After:**
```python
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
```
Why this is better:
- Eliminates double iteration: Old version iterates once for set(), again for list comprehension
- Removes unnecessary list creation: No need to convert set → list just to count
- Better memory usage: Generator expression feeds directly into set constructor
- Same time complexity but more efficient constant factors
The old approach first deduplicates all the characters, then filters alphabetic ones. The new approach filters while building the set, which is the natural order of operations for this problem.1 parent a51a10e commit 1a164c7
1 file changed
+1
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | | - | |
| 45 | + | |
48 | 46 | | |
49 | 47 | | |
50 | 48 | | |
| |||
0 commit comments