Prefix and Suffix Search
Given many words
, words[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的更多相关文章
- [LeetCode] Prefix and Suffix Search 前后缀搜索
Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...
- [Swift]LeetCode745. 前缀和后缀搜索 | Prefix and Suffix Search
Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...
- 745. Prefix and Suffix Search 查找最大index的单词
[抄题]: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...
- 【leetcode】745. Prefix and Suffix Search
题目如下: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...
- <trim>: prefix+prefixOverrides+suffix+suffixOverrides
<trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...
- SpringMVC-组件分析之视图解析器(prefix,suffix)
SpringMVC的默认组件都是在DispatcherServlet.properties配置文件中配置的: spring-webmvc->org/springframewrok/web/ser ...
- Prefix and Suffix
题目描述 Snuke is interested in strings that satisfy the following conditions: The length of the string ...
- 单词的添加与查找 · Add and Search Word
[抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- BZOJ 1257 [CQOI2007]余数之和 数学
都不知道说什么好...咕咕到现在.. 求:$\sum_{i=1}^n \space k\space mod \space i$ 即求:$n*k-\sum_{i=1}^n\space \lfloor \ ...
- Sqlite3错误:Recursive use of cursors not allowed 的解决方案
感悟] 写完这篇日志后,有调了一段时间程序,又有了一点心得分享下: 一)爬稳定的数(dong)据(xi)最好存储下来,特别是数据库在国外的那种,下载时间成本太高昂了,存起来再处理,会节约很多时间: 二 ...
- Zookeeper系列(十三)Zookeeper的请求处理
作者:leesf 掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6179118.html尊重原创,奇文共欣赏: 一.前言 在 ...
- Go 通道(channel)与协程间通信
协程间通信 协程中可以使用共享变量来通信,但是很不提倡这样做,因为这种方式给所有的共享内存的多线程都带来了困难. 在 Go 中有一种特殊的类型,通道(channel),就像一个可以用于发送类型化数据的 ...
- mybatis中添加时间字符串条件
<if test="operatorDateStart != null and operatorDateStart !='' " >operator_date > ...
- Linux设备驱动程序 之 并发及其管理
竞态产生 Linux系统找那个存在大量的并发联系,因此会导致可能的竞态: 1. 正在运行的用户空间进程可以以多种组合方式访问我们的代码: 2. SMP系统甚至可以再不同的处理器上同时执行我们的代码: ...
- 1.分布式配置中心 spring-cloud-config
pring Cloud 版本:2.1.0.RELEASE 一.server端 1.maven依赖 <dependency> <groupId>org.springframewo ...
- Android RecyclerView实现加载多种条目类型
今天咱们是用RecyclerView来实现这个多种Item的加载. 其实最关键的是要复写RecyclerView的Adapter中的getItemViewType()方法 这个方法就根据条件返回条目的 ...
- ubuntu16.04下如何安装dtc工具?
答: sudo apt-get install device-tree-compiler -y
- [转]Nginx配置信息详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...