648. Replace Words
Problem statement
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 wordsuccessor. For example, the rootan, followed byother, which can form another wordanother.Now, given a dictionary consisting of many roots and a sentence. You need to replace all the
successorin the sentence with therootforming it. If asuccessorhas manyrootscan 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"Note:
- The input will only have lower-case letters.
- 1 <= dict words number <= 1000
- 1 <= sentence words number <= 1000
- 1 <= root length <= 100
- 1 <= sentence words length <= 1000
Solution
Although this is the fourth problem of leetcode weekly contest 42, the solution is pretty straightforward.
The best solution should employ the trie tree, by now, I have no idea of that. Alternatively, I add all root words into a hash table and search by the key value.
The general idea:
- Add all roots into hash table.
- Separate the strings into single word.
- Search from the start of each word to find if root exists in hash table.
Time complexity is O(n), n is the size of setence, space complexity is O(n + m), m is the size of root.
class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
// add the dict into a set to search
set<string> ht;
for(auto dic : dict){
ht.insert(dic);
}
// partition the sentences into words
vector<string> words;
for(int i = , j = ; i < sentence.size() && j <= sentence.size(); j++){
if(sentence[j] == ' ' || j == sentence.size()){
words.push_back(sentence.substr(i, j - i));
i = j + ;
}
}
// find root in dictionary
for(int i = ; i < words.size(); i++){
for(int j = ; j < words[i].size(); j++){
if(ht.find(words[i].substr(, j + )) != ht.end()){
words[i] = words[i].substr(, j + );
break;
}
}
}
// build the whole sentence
string ans = words[];
for(int i = ; i < words.size(); i++){
ans += " " + words[i];
}
return ans;
}
};
648. Replace Words的更多相关文章
- 648. Replace Words 替换成为原来的单词
[抄题]: In English, we have a concept called root, which can be followed by some other words to form a ...
- LC 648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- 【LeetCode】648. Replace Words 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...
- LeetCode 648. Replace Words (单词替换)
题目标签:HashMap 题目给了我们一个array 的 root, 让我们把sentence 里面得每一个word 去掉它得 successor. 把每一个root 存入hash set,然后遍历s ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 字典树(Trie)
Trie基础 Trie字典树又叫前缀树(prefix tree),用以较快速地进行单词或前缀查询,Trie节点结构如下: //208. Implement Trie (Prefix Tree)clas ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)
All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...
随机推荐
- 修改完linux bashrc文件之后,如何不重启系统使其生效
修改完后,输入如下命令即可 ##@##:~/ source ~/.bashrc 之后bashrc文件就可以使用! 注: 使用ssh登陆shell的时候,系统不会自动调用.bashrc文件, 只是 ...
- iosopendev配置
Permission denied, please try again.Permission denied, please try again.Permission denied (publickey ...
- Python 学习日志9月19日
9月19日 周二 今天是普通的一天,昨天也是普通的一天,刚才我差点忘记写日志,突然想起来有个事情没做,回来写. 今天早晨学习<Head First HTML and CSS>第十一章节“布 ...
- LinuxShell(脚本如何编译问题)
想学shell的同学请记住: 如果你写好脚本后不给脚本执行权限那也是不行的: 添加执行权限: chmod +x 脚本名.sh 在Linux shell中有一个脚本编译命令: bash -v 脚本名.s ...
- 程序windows上可以上传附件,部署到 linux服务器后出现 “上传目录 不可写” 怎么解决?
这样的问题一般都是linux 下文件读写权限引起的,用 shell 命名到上传附件的目录(如 cd /data/www/project/upload/),然后执行 shell 文件权限设置: 例如 ...
- C# IsNullOrEmpty与IsNullOrWhiteSpace
IsNullOrEmpty:非空非NULL判断 IsNullOrWhiteSpace:非空非NULL非空格判断 后者优于前者 if (!string.IsNullOrWhiteSpace(valueE ...
- 详解Mac睡眠模式设置
详解Mac睡眠模式设置 原文链接:http://www.insanelymac.com/forum/index.php?showtopic=281945 需要说明的是,首先这篇文章是针对已经能够成功睡 ...
- C++系统学习之三:向量
标准库类型vector 定义:vector表示对象的集合,其中所有对象的类型都相同. 访问方式:索引 头文件:<vector> 本质:类模板 NOTE: 模板本身不是类或函数,相反可以将模 ...
- (38)zabbix中配置snmp监控
1.首先按照“snmp监控快速配置”文本文档在被监控的主机上安装.配置及启动snmp服务, 具体内容如下: 1).安装snmp yum install net-snmp* -y cp -a /etc/ ...
- simulation clock gen unit (推荐)
//Normal Clock Block always begin:clk_blk clk <=; # clk<=; #; end //Improved Clock Block, impr ...