Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created May 24, 2025 22:07
Show Gist options
  • Save Ifihan/46e5a847a07c29c49f4ca0c71b5f0a3b to your computer and use it in GitHub Desktop.
Save Ifihan/46e5a847a07c29c49f4ca0c71b5f0a3b to your computer and use it in GitHub Desktop.
Find Words Containing Character

Question

Approach

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.

Implementation

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

Complexities

  • 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.
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment