748. 最短补全词

exiaohu 于 2021-12-10 发布

题目链接:748. 最短补全词

from collections import Counter
from typing import List


class Solution:
    def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
        counter = Counter(map(str.lower, filter(str.isalpha, licensePlate)))

        result, l = '', float('inf')
        for word in words:
            wc = Counter(word)
            wc.subtract(counter)

            if min(wc.values()) >= 0 and len(word) < l:
                result, l = word, len(word)

        return result