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].