Replace Words
In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.
Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.
You need to output the sentence after the replacement.
Example 1:
Input: dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat" 按照题意,基本上能够马上知道这题需要用trie
class Solution {
public String replaceWords(List<String> dict, String sentence) {
String[] tokens = sentence.split(" ");
TrieNode trie = buildTrie(dict);
return replaceWords(tokens, trie);
}
private String replaceWords(String[] tokens, TrieNode root) {
StringBuilder stringBuilder = new StringBuilder();
for (String token : tokens) {
stringBuilder.append(getShortestReplacement(token, root));
stringBuilder.append(" ");
}
return stringBuilder.substring(, stringBuilder.length()-);
}
private String getShortestReplacement(String token, final TrieNode root) {
TrieNode temp = root;
StringBuilder stringBuilder = new StringBuilder();
for (char c : token.toCharArray()) {
stringBuilder.append(c);
if (temp.map.containsKey(c)) {
if (temp.map.get(c).isWord) {
return stringBuilder.toString();
}
temp = temp.map.get(c);
} else {
return token;
}
}
return token;
}
private TrieNode buildTrie(List<String> dict) {
TrieNode root = new TrieNode(' ');
for (String word : dict) {
TrieNode current = root;
for (char ch : word.toCharArray()) {
TrieNode node = current.getChildNode(ch);
if (node == null) {
current.map.put(ch, new TrieNode(ch));
node = current.getChildNode(ch);
}
current = node;
}
current.isWord = true;
}
return root;
}
}
class TrieNode {
char ch;
boolean isWord;
Map<Character, TrieNode> map;
public TrieNode(char ch) {
this.ch = ch;
map = new HashMap<>();
}
public TrieNode getChildNode(char ch) {
return map.get(ch);
}
}
Replace Words的更多相关文章
- <JavaScript语言精粹>--<读书笔记三>之replace()与正则
今天有人问我repalce(),他那个题目很有意思.我也不会做,于是我就去查,结果发现就是最基础的知识的延伸. 所以啊最基础的知识才是很重要的,千万不能忽略,抓起JS就写代码完全不知到所以然,只知道写 ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- js的replace函数入参为function时的疑问
近期在写js导出excel文件时运用到replace方法,此处详细的记录下它各个参数所代表的的意义. 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式 ...
- ORACLE 利用 REPLACE函数替换字段字符串
REPLACE(string,s1,s2) string 希望被替换的字符或变量 s1 被替换的字符串 s2 要替换的字符串 SQL> select replace(he love you,he ...
- js 页面刷新location.reload和location.replace的区别小结
reload 方法,该方法强迫浏览器刷新当前页面. 语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前 ...
- replace和translate的用法
select replace ('111222333444','222','888') from dual;with tmp as(select 'aabb/123\:cde工人' s from du ...
- JavaScript replace() 方法
参考:http://www.w3school.com.cn/jsref/jsref_replace.asp 需要有一点注意的是:可以是函数的形式做为返回值,如下: "test{0}" ...
- replace实现正则过滤替换非法字符
html+js结构如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- Replace 删除、替换函数精解示例
'************************************************************************* '**模 块 名:Replace函数精解示例 '* ...
- angularjs 指令详解 - template, restrict, replace
通过指令机制,angularjs 提供了一个强大的扩展系统,我们可以通过自定义指令来扩展自己的指令系统. 怎样定义自己的指令呢? 我们通过 Bootstrap UI来学习吧.这个项目使用 angula ...
随机推荐
- 状压dpHDU - 4856
J - Tunnels HDU - 4856 题目大意:地图上有些管道,在管道行走里不需要花费时间,但从一个管道的出口走到另一个管道的入口则需要花费时间,问走完所有管道最短的时间,如果不行,则输出-1 ...
- 也谈Tcp/Ip协议
一. 计算机网络体系结构分层 一图看完本文 计算机网络体系结构分层 计算机网络体系结构分层 不难看出,TCP/IP 与 OSI 在分层模块上稍有区别.OSI 参考模型注重“通信协议必要的功能是什么”, ...
- php phpexcel 创建excel
public function createExcel($result=[],$fileName=''){ \think\Loader::import('extend.excel.PHPExcel', ...
- Nginx-rtmp之配置项的管理
1. 概述 Nginx-rtmp 对 rtmp{...} 内的配置项划分了几个级别: 直接隶属于 rtmp{} 块内的配置项称为 main 配置项. 直接隶属于 server{} 块内的配置项称为 s ...
- Nginx之监控进程和工作进程
1. 函数调用分析 在开启 master 的情况下,多进程模型的下的入口函数为 ngx_master_process_cycle,如下: int mian() { ... if (ngx_proces ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- 按下home键,重新打开,应用重启
其实不是重启,只是重新打开了luncher的那个activity.只要通过判断把它finish,就会显示按下home键前的页面. 解决方法: 在重启的页面中加入一下代码,注意加在setContentV ...
- js 中文字符串转base64
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mongodb增删改查操作
Note:mongodb存储的是文档,且文档是json格式的对象,所以增删改查都必须是json格式对象. 注:mongodb常用库和表操作,但mongodb在插入数据时,不需要先创建表. show d ...
- Javascript 二维码生成库:QRCode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...