I iterate through each word using enumerate to keep track of the index. If the character x is found in the word, I add the index to the result list. Then, I return the list of indices.
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
result = []
for i, word in enumerate(words):
if x in word:
result.append(i)
return result
- Time: O(n * m), where n is the number of words and m is the average word length.
- Space: O(k), where k is the number of matching indices added to the result.
