Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

Each word can be checked independently. A word contributes its index if the target character appears anywhere in it; the number of occurrences does not matter.

Approach

Visit the words in their original order. For each word, use a character-membership check to determine whether it contains x. Append its index when the check succeeds and continue to the next word.

Return the collected indices. Checking every word ensures none is missed, and adding at most one index per word prevents duplicates when x occurs multiple times. The traversal naturally returns indices in increasing order, and an empty list is returned when no word contains the character.

class Solution: def findWordsContaining(self, words: List[str], x: str) -> List[int]: n = len(words) res = [] for i in range(n): if x in words[i]: res.append(i) return res

Complexity analysis

Let C be the total number of characters across all words. Time: O(C). Auxiliary space: O(1) excluding the returned index list, which is O(words.length) in the worst case.