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. Word:自动编号超过9后缩进太大

     造冰箱的大熊猫,本文适用于Microsoft Office 2007@cnblogs 2019/7/30 文中图片可通过点击鼠标右键查看大图 1.场景 如下图所示,使用Word的自动编号功能时,当编 ...

  2. 北京清北 综合强化班 Day2

    a [问题描述] 你是能看到第一题的 friends呢.                                                —— hja世界上没有什么比卖的这 贵弹丸三还令 ...

  3. Wox使用指南

    下载安装 从下载地址下载最新版本的 wox ,我下载的是 exe 版的 Wox-1.3.578.exe 下载以后直接安装即可,不会有选择项,安装成功以后会在屏幕上出现一个搜索框,默认失去焦点以后搜索框 ...

  4. docker下的images 保存和导出

    由于迁移测试环境,所以部署的的docker镜像某一些需要迁移到另外一台服务器上面去.. 我是用 docker save -o registry.tar registry 来把相应需要导出的images ...

  5. 关于keepalive

    linux内核配置有一项tcp_keepalive_time,即tcp的保活定时器.当网络上两个建立连接的进程都没有数据向对方发送的时候,tcp会隔段时间发送一次保活数据,以保持连接,间隔时间就是tc ...

  6. centos 安装mariadb 替代mysql

    yum install mariadb-server mariadb systemctl start mariadbmysql -uroot -p默认密码mysql -uroot -pmysql_se ...

  7. DIV盒子模型介绍 div用法

  8. 网络流,设备、插头和转接器建图(简单map的应用)

    题意: 给你n个插座,m个设备,每台设备都有对应的插座,有k个转接器. 要求:求满足不能插上插座的用电器最少个数 solution: HINT:每种适配器都有无限个,所以建图的时候要改为INF. 答案 ...

  9. Laravel 代码开发最佳实践

    我们这里要讨论的并不是 Laravel 版的 SOLID 原则(想要了解更多 SOLID 原则细节查看这篇文章)亦或是设计模式,而是 Laravel 实际开发中容易被忽略的最佳实践. 内容概览 单一职 ...

  10. POJ 1837 -- Balance(DP)

     POJ 1837 -- Balance 转载:優YoU   http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...