749 Shortest Completing Word
1. Question
Find the minimum length word from a given dictionarywords
, which has all the letters from the stringlicensePlate
. Such a word is said tocompletethe given stringlicensePlate
Here, for letters we ignore case. For example,"P"
on thelicensePlate
still matches"p"
on the word.
It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.
The license plate might have the same letter occurring multiple times. For example, given alicensePlate
of"PP"
, the word"pair"
does not complete thelicensePlate
, but the word"supper"
does.
Example 1:
Example 2:
Note:
licensePlate
will be a string with length in range[1, 7]
.licensePlate
will contain digits, spaces, or letters (uppercase or lowercase).words
will have a length in the range[10, 1000]
.Every
words[i]
will consist of lowercase letters, and have length in range[1, 15]
.
2. Implementation
(1) Hash Table
3. Time & Space Complexity
(1) Hash Table: 时间复杂度O(nL), n是word的个数,L是words里的word的平均长度,空间复杂度O(1)
Last updated