class WordFilter {
public:
WordFilter(vector<string>& words) {
}
int f(string pref, string suff) {
}
};
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter* obj = new WordFilter(words);
* int param_1 = obj->f(pref,suff);
*/
class WordFilter {
Trie trie;
String weightKey;
public WordFilter(String[] words) {
trie = new Trie();
weightKey = "##";
for (int i = 0; i < words.length; i++) {
String word = words[i];
Trie cur = trie;
int m = word.length();
for (int j = 0; j < m; j++) {
Trie tmp = cur;
for (int k = j; k < m; k++) {
String key = new StringBuilder().append(word.charAt(k)).append('#').toString();
if (!tmp.children.containsKey(key)) {
tmp.children.put(key, new Trie());
}
tmp = tmp.children.get(key);
tmp.weight.put(weightKey, i);
}
tmp = cur;
for (int k = j; k < m; k++) {
String key = new StringBuilder().append('#').append(word.charAt(m - k - 1)).toString();
if (!tmp.children.containsKey(key)) {
tmp.children.put(key, new Trie());
}
tmp = tmp.children.get(key);
tmp.weight.put(weightKey, i);
}
String key = new StringBuilder().append(word.charAt(j)).append(word.charAt(m - j - 1)).toString();
if (!cur.children.containsKey(key)) {
cur.children.put(key, new Trie());
}
cur = cur.children.get(key);
cur.weight.put(weightKey, i);
}
}
}
public int f(String pref, String suff) {
Trie cur = trie;
int m = Math.max(pref.length(), suff.length());
for (int i = 0; i < m; i++) {
char c1 = i < pref.length() ? pref.charAt(i) : '#';
char c2 = i < suff.length() ? suff.charAt(suff.length() - 1 - i) : '#';
String key = new StringBuilder().append(c1).append(c2).toString();
if (!cur.children.containsKey(key)) {
return -1;
}
cur = cur.children.get(key);
}
return cur.weight.get(weightKey);
}
}
class Trie {
Map<String, Trie> children;
Map<String, Integer> weight;
public Trie() {
children = new HashMap<String, Trie>();
weight = new HashMap<String, Integer>();
}
}
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter obj = new WordFilter(words);
* int param_1 = obj.f(pref,suff);
*/
# 字典树
class WordFilter:
def __init__(self, words: List[str]):
self.trie = {}
self.weightKey = ('#', '#')
for i, word in enumerate(words):
cur = self.trie
m = len(word)
for j in range(m):
tmp = cur
for k in range(j, m):
key = (word[k], '#')
if key not in tmp:
tmp[key] = {}
tmp = tmp[key]
tmp[self.weightKey] = i
tmp = cur
for k in range(j, m):
key = ('#', word[-k - 1])
if key not in tmp:
tmp[key] = {}
tmp = tmp[key]
tmp[self.weightKey] = i
key = (word[j], word[-j - 1])
if key not in cur:
cur[key] = {}
cur = cur[key]
cur[self.weightKey] = i
def f(self, pref: str, suff: str) -> int:
cur = self.trie
for key in zip_longest(pref, suff[::-1], fillvalue='#'):
if key not in cur:
return -1
cur = cur[key]
return cur[self.weightKey]
# Your WordFilter object will be instantiated and called as such:
# obj = WordFilter(words)
# param_1 = obj.f(pref,suff)
class WordFilter {
Map<String, Integer> dictionary;
public WordFilter(String[] words) {
dictionary = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++) {
String word = words[i];
int m = word.length();
for (int prefixLength = 1; prefixLength <= m; prefixLength++) {
for (int suffixLength = 1; suffixLength <= m; suffixLength++) {
dictionary.put(word.substring(0, prefixLength) + "#" + word.substring(m - suffixLength), i);
}
}
}
}
public int f(String pref, String suff) {
return dictionary.getOrDefault(pref + "#" + suff, -1);
}
}
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter obj = new WordFilter(words);
* int param_1 = obj.f(pref,suff);
*/
type WordFilter map[string]int
func Constructor(words []string) WordFilter {
wf := WordFilter{}
for i, word := range words {
for j, n := 1, len(word); j <= n; j++ {
for k := 0; k < n; k++ {
wf[word[:j]+"#"+word[k:]] = i
}
}
}
return wf
}
func (wf WordFilter) F(pref, suff string) int {
if i, ok := wf[pref+"#"+suff]; ok {
return i
}
return -1
}
/**
* Your WordFilter object will be instantiated and called as such:
* obj := Constructor(words);
* param_1 := obj.F(pref,suff);
*/
# 暴力搜索
class WordFilter:
def __init__(self, words: List[str]):
self.d = {}
for i, word in enumerate(words):
m = len(word)
for prefixLength in range(1, m + 1):
for suffixLength in range(1, m + 1):
self.d[word[:prefixLength] + '#' + word[-suffixLength:]] = i
def f(self, pref: str, suff: str) -> int:
return self.d.get(pref + '#' + suff, -1)
# Your WordFilter object will be instantiated and called as such:
# obj = WordFilter(words)
# param_1 = obj.f(pref,suff)