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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 记录二:tensorflow2.0写MNIST手写体
最近学习神经网络,tensorflow,看了好多视频,查找了好多资料,感觉东西都没有融入自己的思维中.今天用tensorflow2.0写了一个MNIST手写体的版本,记录下学习的过程. 复现手写体识别 ...
- codeforces gym #101161H - Witcher Potion(状压DP)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 总共有n瓶药可供选择 每瓶药可以增加$e_i$点体力,和$p_i$点毒性 每分钟消耗1点毒 ...
- Raspberry Pi 4B基本设置
目录 一.SSH登录Raspberry Pi 二.开启VNC服务 三.将SD卡分区扩展 四.修改软件源 一.SSH登录Raspberry Pi 完成系统烧录后,就需要登录Raspberry Pi,此时 ...
- centos7下面改变亮度
通过改自己的配置文件来改变亮度的方法在我的机器上面是不中用了,我的是台式机,我用的集成显卡,目前我找到的还勉强的方法就是通过安装redshift来改变屏幕的亮度,我就是不喜欢太亮,差点瞎了. 具体是这 ...
- 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.
用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...
- IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(一)
新人接触springboot,IDE使用的是IntelliJ IDEA 2017.3 ,自己摸索了很久,现在自己整理一下,里面有些操作我自己也不懂是为什么这样,只是模仿公司现有的项目,自己搭建了一个简 ...
- Flume-Taildir Source 监控目录下多个文件的追加
Exec source 适用于监控一个实时追加的文件,但不能保证数据不丢失:Spooldir Source 能够保证数据不丢失,且能够实现断点续传,但延迟较高,不能实时监控:而 Taildir Sou ...
- 浅谈WebViewClient与WebChromeClient
简介:WebViewClient被用来传递单纯的加载一个链接时所发生的事件,比如开始加载,结束加载等,它代表这个链接加载时的最普通的和最笼统的事件,WebChromeClient更多的是传递JS对话框 ...
- ResourceUtils 创建资源目录工具类
package com.jcf.utilsdemo; import android.content.Context; import android.content.res.Resources; pub ...
- [Kaggle] How to kaggle?
成立于2010年的Kaggle是一个进行数据发掘和预测竞赛的在线平台.与Kaggle合作之后,一家公司可以提供一些数据,进而提出一个问题,Kaggle网站上的计算机科学家和数学家,也就是现在所说的数据 ...