[
hash table
counter
]
Leetcode 0748 Shortest Completing Word
Problem statement
https://leetcode.com/problems/shortest-completing-word/
Solution
Just use counters and lower
functionality. Iterate over words
and check if it is good, using difference of counters trick.
Complexity
Time and space complexity is O(m)
, where m
is total length of all words in words
and licensePlate
.
Code
class Solution:
def shortestCompletingWord(self, licensePlate, words):
count = Counter("".join(s for s in licensePlate.lower() if s.isalpha()))
return min([w for w in words if not count - Counter(w)], key = len)