S2Q1 · Words with Consecutive Identical Letters¶
⚡ Quick Reference
Function: words_with_consecutive_letters(words: list) -> list
Core idea: for each word, check if any two adjacent characters are the same (case-insensitive).
def words_with_consecutive_letters(words):
def has_consecutive(word):
w = word.lower()
return any(w[i] == w[i+1] for i in range(len(w) - 1))
return [w for w in words if has_consecutive(w)]
Key rules:
- Case-insensitive → lowercase before comparing
- Check adjacent pairs: word[i] == word[i+1]
- Preserve original order
- any() short-circuits on the first matching pair
Problem Statement¶
Problem
Write a function words_with_consecutive_letters(words) that returns all words containing at least one pair of consecutive identical letters, in their original order.
Examples:
["hello", "apple", "ball", "test", "cat"]
["hello", "apple", "ball"]
["sky", "fly", "run", "jump"]
[]
Tracing example 1¶
| Word | Pairs checked | Consecutive pair? | Include? |
|---|---|---|---|
"hello" |
he, el, ll✅, lo | ll |
✅ |
"apple" |
ap, pp✅, pl, le | pp |
✅ |
"ball" |
ba, al, ll✅ | ll |
✅ |
"test" |
te, es, st | none | ❌ |
"cat" |
ca, at | none | ❌ |
Result: ["hello", "apple", "ball"] ✓
Solution approaches¶
Key takeaways¶
zip(w, w[1:]) - elegant adjacent pair iteration
zip(w, w[1:]) pairs each character with the next one without index arithmetic. It naturally stops one pair before the end - no off-by-one risk.
any() short-circuits - stops at first match
any(a==b for a,b in zip(w, w[1:])) stops checking as soon as the first consecutive pair is found. No need to scan the whole word once a match is confirmed.
Lowercase for comparison, return original case
Apply .lower() only for the comparison - keep the original word in the result list. has_consec(w.lower()) checks the lowercased version while the list comprehension appends the original w.