class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
res = ''
dict = [0] * 26
#建立字典
for ch in licensePlate:
if ch.isalpha():
dict[ord(ch.lower()) - 97] += 1
for word in words:
tmp = list(dict) #复制一份用于检测
for ch in word:
tmp[ord(ch.lower()) - 97] -= 1
#符合plate,并且长度最小
if self.metLicense(tmp) and (len(word) < len(res) or res == ''):
res = word
return res
#辅助函数,只要数组中有一个>0元素,返回false
def metLicense(self, nums):
return not any([x>0 for x in nums])