Given many wordswords[i] has weight i.

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:

Input:
WordFilter(["apple"])
WordFilter.f("a", "e") // returns 0
WordFilter.f("b", "") // returns -1 分析:因为要找出word一定begin with prefix,以及end with suffix,有点麻烦。其实找begin with prefix不麻烦,麻烦的是怎么找end with suffix. 这里有点巧的方法是对于每个word,把它所有的
suffx_word放在trie里,比如apple, 我们就把 "_apple", "e_apple", "le_apple", "ple_apple", "pple_apple", "apple_apple" 都存在trie里。这样可以用trie把所有的情况都包括了。
 class TrieNode {
char ch;
int weight;
Map<Character, TrieNode> map; public TrieNode(char ch, int weight) {
this.ch = ch;
this.weight = weight;
map = new HashMap<>();
} public TrieNode getChildNode(char ch) {
return map.get(ch);
}
} public class WordFilter {
private TrieNode root = new TrieNode(' ', -);
public WordFilter(String[] words) {
for (int weight = ; weight < words.length; weight++) {
List<String> suffixList = generateSuffixList(words[weight]);
for (String word : suffixList) {
addWord(word, weight);
}
}
} public void addWord(String word, int weight) {
TrieNode current = root;
for (int i = ; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.getChildNode(ch);
if (node == null) {
current.map.put(ch, new TrieNode(ch, weight));
node = current.getChildNode(ch);
}
node.weight = Math.max(weight, node.weight);
current = node;
}
}
private List<String> generateSuffixList(String word) {
List<String> suffixList = new ArrayList<>();
int length = word.length();
for (int i = length; i >= ; i--) {
String sub = word.substring(i, length);
suffixList.add(sub + "_" + word);
}
return suffixList;
} public int f(String prefix, String suffix) {
if (root == null) return -;
TrieNode current = root; String word = suffix + "_" + prefix;
for (char ch : word.toCharArray()) {
current = current.getChildNode(ch);
if (current == null) {
return -;
}
}
return current.weight;
}
}

Prefix and Suffix Search的更多相关文章

  1. [LeetCode] Prefix and Suffix Search 前后缀搜索

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  2. [Swift]LeetCode745. 前缀和后缀搜索 | Prefix and Suffix Search

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  3. 745. Prefix and Suffix Search 查找最大index的单词

    [抄题]: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...

  4. 【leetcode】745. Prefix and Suffix Search

    题目如下: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...

  5. <trim>: prefix+prefixOverrides+suffix+suffixOverrides

    <trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...

  6. SpringMVC-组件分析之视图解析器(prefix,suffix)

    SpringMVC的默认组件都是在DispatcherServlet.properties配置文件中配置的: spring-webmvc->org/springframewrok/web/ser ...

  7. Prefix and Suffix

    题目描述 Snuke is interested in strings that satisfy the following conditions: The length of the string ...

  8. 单词的添加与查找 · Add and Search Word

    [抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 013_linuxC++之_派生类中权限的调整

    (一)在上一篇012_LINUXC++之_类的继承定义中我们知道在派生类中可以访问public和protectd中的数据 (二)那么我们就可以在派生类中将上面两个中的数据进行权限的修改 (三)程序 # ...

  2. apply, bind, call--绑定this的方法

    Function.prototype.call(),Function.prototype.apply(),Function.prototype.bind() 是三种改变函数内部this指向(即函数执行 ...

  3. noi 第n小的质数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个正整数n,求第n小的质数. 输入 一个不超过10000的正整数n. 输出 第n小的质数. 样例输入 10 样例输出 29 一定 ...

  4. 从斐波那契数列看java方法的调用过程

    先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...

  5. 看透“0”、“1”逻辑,轻松解决Python中文乱码

    Python中关于"中文乱码"的问题,现规整如下,并统一回答 同学问: jacky:我在爬取XX网站信息的时候,中文怎么总是显示的乱码? jacky:UTF-8与GBK到底是个啥? ...

  6. mysql被收购 用mariadb

    ~]# systemctl start mysql.service 要启动MySQL数据库是却是这样的提示 Failed to start mysqld.service: Unit not found ...

  7. Java基础线程系列大纲

    ## Java 多线程之 线程创建 ## Java 多线程之 Sleep ## Java 多线程之 Join ## Java 多线程之 生命周期 ## Java 多线程之 wait, notify a ...

  8. Netfilter 之 钩子函数调用

    本篇主要从三层协议栈调用函数NF_HOOK说起,不断深入,分析某个钩子点中所有钩子函数的调用流程,但是本文不包含规则介绍和核心的规则匹配流程,后续文章将继续分析: NF_HOOK函数先调用了nf_ho ...

  9. Windows Server 2012R2 部署 Domain Controller

    1. Create a machine as Domain Controller; 2. Change DNS server address as 127.0.0.1; 3. Change Compu ...

  10. Java 比较两个字符串的相似度算法(Levenshtein Distance)

    转载自: https://blog.csdn.net/JavaReact/article/details/82144732 算法简介: Levenshtein Distance,又称编辑距离,指的是两 ...