这题是水题,只需要统计字符串每个字母出现的次数,然后比较一下,一个包含另一个就行了,注意是包含。附代码
class Solution:
def shortestCompletingWord(self, licensePlate, words):
"""
:type licensePlate: str
:type words: List[str]
:rtype: str
"""
def sumNumber(str):
t = {}
for i in str:
if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
if i in t:
t[i] +=1
else:
t[i] = 1
return t
licenseValue = sumNumber(licensePlate.lower())
word_result = "2"*1000
for word in words:
temp = sumNumber(word.lower())
sign = True
for item,value in licenseValue.items():
if item not in temp or temp[item] < value:
sign = False
break
if sign:
if len(word) < len(word_result):
word_result = word
return word_result