leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

find_words_contain_char.py (763B)


# 2942. Find Words Containing Character

"""
you are given a 0-indexed array of strings 'words' and a character 'x'.
return an array of indices representing the words that contain the character
'x'. note that the returned array may be in any order.
"""


class Solution(object):
    def findWordsContaining(self, words, x):
        """
        :type words: List[str]
        :type x: str
        :rtype: List[int]
        """
        return [i for i in range(len(words)) if x in words[i]]


if __name__ == "__main__":
    obj = Solution()
    print(obj.findWordsContaining(words=["leet", "code"], x="e"))
    print(obj.findWordsContaining(words=["abc", "bcd", "aaaa", "cbc"], x="a"))
    print(obj.findWordsContaining(words=["abc", "bcd", "aaaa", "cbc"], x="z"))