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 ...
随机推荐
- 查询sqlserver数据库,表占用数据大小
if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')dro ...
- webpack前端构建工具学习总结(三)之webpack.config.js配置文件
Webpack 在执行的时候,除了在命令行传入参数,还可以通过指定的配置文件来执行.默认情况下,会搜索当前目录的 webpack.config.js 文件,这个文件是一个 node.js 模块,返回一 ...
- FZU 1977 Pandora adventure (插头DP,常规)
题意:有一个n*m矩阵,其中有些格子必走,有些格子不可走,其他格子是可走也可不走,问有多少条哈密顿回路? 思路: 本来是一道很简单的题,代码写多了连白痴bug都查不出了,竟然用i>=ex& ...
- codevs 2915 期末考试
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 期末考试要来了,某同学正在努力复习. 他要复习N个知识点,每个知识点需要一定的知识 ...
- iosopendev配置
Permission denied, please try again.Permission denied, please try again.Permission denied (publickey ...
- CNNs 在图像分割中应用简史: 从R-CNN到Mask R-CNN
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6756024.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- UVA - 12264 Risk (二分,网络流)
题意比较坑,移动完以后的士兵不能再次移动,不然样例都过不了... 最小值最大满足决策单调性所以二分答案,跑网络流验证是否可行. 这种题重点在建图,为了保证只移动一次,拆点,一个入点一个出点,到了出点的 ...
- SpringMVC、Spring和Struts的区别
http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...
- 卷积网络中的通道(Channel)和特征图
转载自:https://www.jianshu.com/p/bf8749e15566 今天介绍卷积网络中一个很重要的概念,通道(Channel),也有叫特征图(feature map)的. 首先,之前 ...
- grep, egrep, fgrep - 打印匹配给定模式的行
总览 SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 描述 DESC ...