Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nerdyalgorithm/f9d3637d47344f7a495f10e65eff7f7f to your computer and use it in GitHub Desktop.

Select an option

Save nerdyalgorithm/f9d3637d47344f7a495f10e65eff7f7f to your computer and use it in GitHub Desktop.
leetcode-2942-find words containing character

🧠 Approach

Given a list of words and a character x, we need to return the indices of the words that contain that character.

I used a basic loop with enumerate() to track both the index and the word. If the character x is found in the word (if x in word:), I store the index.

βœ… Solution

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

πŸ’¬ Example

words = ["leet", "code"]
x = "e"
# Output: [0, 1] since both "leet" and "code" contain "e"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment