leetpycode/medium/word_subsets.py

17 lines
571 B
Python

# https://leetcode.com/problems/word-subsets
from typing import Counter, Dict, List
class Solution:
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
result = []
count: Dict[str, int] = {}
for word2 in words2:
c = Counter(word2)
for k, v in c.items():
count[k] = max(count.setdefault(k, 0), v)
for word1 in words1:
c = Counter(word1)
if all(c.get(k, 0) >= v for k, v in count.items()):
result.append(word1)
return result