Find the minimum length word from a given dictionaryĀ words, which has all the letters from the stringĀ licensePlate. Such a word is said toĀ completeĀ the given stringĀ licensePlate
Here, for letters we ignore case. For example,Ā "P"Ā on theĀ licensePlateĀ 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 aĀ licensePlateĀ ofĀ "PP", the wordĀ "pair"Ā does not complete theĀ licensePlate, but the wordĀ "supper"Ā does.
Design a classĀ WordFilterĀ that supports one function,Ā WordFilter.f(String prefix, String suffix). It will return the word with givenĀ prefixĀ andĀ suffixĀ with maximum weight. If no word exists, return -1.
Examples:
1
2
3
4
Input:
WordFilter(["apple"])
WordFilter.f("a","e")// returns 0
WordFilter.f("b","")// returns -1
Note:
wordsĀ has length in rangeĀ [1, 15000].
For each test case, up toĀ words.lengthĀ queriesĀ WordFilter.fĀ may be made.
words[i]Ā has length in rangeĀ [1, 10].
prefix, suffixĀ have lengths in rangeĀ [0, 10].
words[i]Ā andĀ prefix, suffixĀ queries consist of lowercase letters only.
Idea:
Construct all possible filters
Solution1:
C++
Time complexity: O(NL^3 + QL)Ā where N is the number of words, L is the max length of the word, Q is the number of queries.
Space complexity: O(NL^3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Author: Huahua
// Runtime: 482 ms
classWordFilter{
public:
WordFilter(constvector<string>& words) {
int index = 0;
for(conststring& word : words) {
int n = word.length();
stringprefix;
for(inti=0;i<=n;++i){
if(i>0)prefix+=word[i-1];
stringsuffix;
for(intj=n;j>=0;--j){
if(j!=n)suffix=word[j]+suffix;
conststringkey=prefix+"_"+suffix;
filters_[key]=index;
}
}
++index;
}
}
intf(stringprefix,stringsuffix){
conststringkey=prefix+"_"+suffix;
auto it=filters_.find(key);
if(it!=filters_.end())returnit->second;
return-1;
}
private:
unordered_map<string,int>filters_;
};
Version #2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Author: Huahua
// Runtime: 499 ms
classWordFilter{
public:
WordFilter(constvector<string>& words) {
int index = 0;
for(conststring& word : words) {
int n = word.length();
vector<string>prefixes(n+1,"");
vector<string>suffixes(n+1,"");
for(inti=0;i<n;++i){
prefixes[i+1]=prefixes[i]+word[i];
suffixes[i+1]=word[n-i-1]+suffixes[i];
}
for(conststring& prefix : prefixes)
for (const string& suffix : suffixes)
filters_[prefix + "_" + suffix] = index;
++index;
}
}
intf(stringprefix,stringsuffix){
conststringkey=prefix+"_"+suffix;
auto it=filters_.find(key);
if(it!=filters_.end())returnit->second;
return-1;
}
private:
unordered_map<string,int>filters_;
};
Solution 2:
C++ / Trie
Time complexity: O(NL^2 + QL)Ā where N is the number of words, L is the max length of the word, Q is the number of queries.
Space complexity: O(NL^2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Author: Huahua
// Runtime: 572 ms
classTrie{
public:
/** Initialize your data structure here. */
Trie():root_(newTrieNode()){}
/** Inserts a word into the trie. */
voidinsert(conststring& word, int index) {
TrieNode* p = root_.get();
for(constcharc:word){
if(!p->children[c-'a'])
p->children[c-'a']=newTrieNode();
p=p->children[c-'a'];
// Update index
p->index=index;
}
p->is_word=true;
}
/** Returns the index of word that has the prefix. */
Given two sentencesĀ words1, words2Ā (each represented as an array of strings), and a list of similar word pairsĀ pairs, determine if two sentences are similar.
For example,Ā words1 = ["great", "acting", "skills"]Ā andĀ words2 = ["fine", "drama", "talent"]Ā are similar, if the similar word pairs areĀ pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].
Note that the similarity relationĀ isĀ transitive. For example, if “great” and “good” are similar, and “fine” and “good” are similar, then “great” and “fine”Ā are similar.
Similarity is also symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.
Also, a word is always similar with itself. For example, the sentencesĀ words1 = ["great"], words2 = ["great"], pairs = []Ā are similar, even though there are no specified similar word pairs.
Finally, sentences can only be similar if they have the same number of words. So a sentence likeĀ words1 = ["great"]Ā can never be similar toĀ words2 = ["doubleplus","good"].
Note:
The length ofĀ words1Ā andĀ words2Ā will not exceedĀ 1000.
The length ofĀ pairsĀ will not exceedĀ 2000.
The length of eachĀ pairs[i]Ā will beĀ 2.
The length of eachĀ words[i]Ā andĀ pairs[i][j]Ā will be in the rangeĀ [1, 20].
Given two sentencesĀ words1, words2Ā (each represented as an array of strings), and a list of similar word pairsĀ pairs, determine if two sentences are similar.
For example, “great acting skills” and “fine drama talent” are similar, if the similar word pairs areĀ pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].
Note that the similarity relation is not transitive. For example, if “great” and “fine” are similar, and “fine” and “good” are similar, “great” and “good” areĀ notĀ necessarily similar.
However, similarity is symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.
Also, a word is always similar with itself. For example, the sentencesĀ words1 = ["great"], words2 = ["great"], pairs = []Ā are similar, even though there are no specified similar word pairs.
Finally, sentences can only be similar if they have the same number of words. So a sentence likeĀ words1 = ["great"]Ā can never be similar toĀ words2 = ["doubleplus","good"].
Note:
The length ofĀ words1Ā andĀ words2Ā will not exceedĀ 1000.
The length ofĀ pairsĀ will not exceedĀ 2000.
The length of eachĀ pairs[i]Ā will beĀ 2.
The length of eachĀ words[i]Ā andĀ pairs[i][j]Ā will be in the rangeĀ [1, 20].