diff options
Diffstat (limited to 'word_subsets.py')
-rw-r--r-- | word_subsets.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/word_subsets.py b/word_subsets.py new file mode 100644 index 0000000..dac7b6a --- /dev/null +++ b/word_subsets.py @@ -0,0 +1,39 @@ +# 916. Word Subsets +from collections import Counter +from functools import reduce +import operator + +""" +you are given two string arrays 'words1' and 'words2'. a string 'b' is a +subset of string 'a' if every letter in 'b' occurs in 'a' including +multiplicity. a string 'a' from 'words1' is universal if for every string 'b' +in 'words2', 'b' is a subset of 'a'. return an array of all the universal +strins in 'words1'. you may return the answer in any order. +""" + + +class Solution(object): + def wordSubsets(self, words1, words2): + """ + :type words1: List[str] + :type words2: List[str] + :rtype: List[str] + """ + c_b = reduce(operator.or_, (Counter(w) for w in words2)) + return [a for a in words1 if c_b & Counter(a) == c_b] + + +if __name__ == "__main__": + obj = Solution() + print( + obj.wordSubsets( + words1=["amazon", "apple", "facebook", "google", "leetcode"], + words2=["e", "o"], + ) + ) + print( + obj.wordSubsets( + words1=["amazon", "apple", "facebook", "google", "leetcode"], + words2=["l", "e"], + ) + ) |