LC 648. 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"
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
Runtime: 64 ms, faster than 45.77% of C++ online submissions for Replace Words.
#include <vector>
#include <string>
using namespace std; class TrieNode{
public:
string word;
TrieNode* children[];
TrieNode(){
for(int i=; i<; i++) children[i] = nullptr;
word = "";
}
};
class Trie{
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
void buildtrie(vector<string>& words){
for(int i=; i<words.size(); i++){
TrieNode* tmp = root;
for(int j=; j<words[i].size(); j++){
int idx = words[i][j] - 'a';
if(!tmp->children[idx]) tmp->children[idx] = new TrieNode();
tmp = tmp->children[idx];
}
tmp->word = words[i];
}
} string hasprefix(string word){
TrieNode* tmp = root;
string prefix = "";
//cout << word << endl;
for(int i=; i<word.size(); i++){
int idx = word[i] - 'a';
//cout << word << " " << idx << endl;
if(!tmp->children[idx]) return prefix;
tmp = tmp->children[idx];
if(tmp->word != ""){
//cout << prefix << endl;
prefix = tmp->word;
break;
}
}
return prefix;
} }; class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
Trie trie = Trie();
trie.buildtrie(dict);
vector<int> spaceidx;
for(int i=; i<sentence.size(); i++){
if(sentence[i] == ' ') spaceidx.push_back(i);
}
vector<string> sent_vec;
int idx = ;
if(spaceidx.size() == ){
sent_vec.push_back(sentence);
}else{
for(int i=; i<spaceidx.size(); i++){
sent_vec.push_back(sentence.substr(idx, spaceidx[i] - idx));
idx = spaceidx[i] + ;
}
}
//for(auto x : spaceidx) cout << x << endl;
sent_vec.push_back(sentence.substr(idx));
//cout << sent_vec.size() << endl;
//for(auto s : sent_vec) cout << s << endl;
vector<string> retvec; for(int i=; i<sent_vec.size(); i++){
string tmpstr = trie.hasprefix(sent_vec[i]);
//cout << sent_vec[i] << endl;
if(tmpstr == "") retvec.push_back(sent_vec[i]);
else retvec.push_back(tmpstr);
}
string ret = "";
for(auto s : retvec) ret += s + " ";
return ret.substr(,ret.size()-);
}
};
LC 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 ...
- 648. Replace Words
Problem statement In English, we have a concept called root, which can be followed by some other wor ...
- 【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 ...
- pt-online-schema-change 最佳实践(转)
pt的详细步骤 Step 1: Create the new table. Step 2: Alter the new, empty table. This should be very quick, ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 单点登录系统SSO实现
前些天被问到单点登录了,而据我当时做的这个模块两年了,现在重新温习并记录下,方便以后快速回忆起来 一.什么是单点登录系统 SSO全称Single Sign On.SSO是用户只需要登录一次就可以访问所 ...
- Win10系统如何利用蓝牙设置动态锁?
很多小伙伴都会有这样的经历,出门之后没走多远,却已然忘记是否锁门,有强迫症的人就会重新返回查看,以确保门是否反锁. 我们在使用电脑时也是这样,遇到事情要临时离开,却忘记是否锁屏,再返回来就耽误时间了. ...
- linux的top下buffer与cache的区别、free命令内存解释
buffer: 缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据 的区域.通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据 时,速度快的设备的操作进程不发 ...
- 获取iframe子页面内容高度给iframe动态设置高度
<!DOCTYPE html><html> <head> <meta charset="UTF-8" /> <meta nam ...
- Python中的字符串及其相关操作
1.表示: 字符串可以用单引号或者双引号括起来,两者效果是完全一样的. 针对较长的字符串,也可以用三个引号括起来,即"""..."""或者' ...
- unittest 详解
内容总括 一. 初始化 setUp 与 tearDown setUpClass 与 tearDownClass unittest.main(verbosity=0/1/2) 二. 执行顺序 按顺序 ...
- sysbench运行autogen.sh报错
./autogen.sh: 4: autoreconf: not found是在不同版本的 tslib 下执行 autogen.sh 产生.它们产生的原因一样,是因为没有安装automake 工具, ...
- 放大镜如何用js
例如: let imgs = { small: ["imgA_1.jpg", "imgB_1.jpg", "imgC_1.jpg"], mi ...
- Vue 实现 登陆后打开主页面(登陆组件 + 主页面组件)
本次演示,项目所需iview,router 首先 在 views 目录 新建 两个 组件 ( login.vue ,index.vue ) login.vue <template> < ...
- macOS Mojave 10.14上安装iTunes12.6
将一下内容保存为iTunes.scpt,并运行 set question to display dialog "确定是否删除 iTunes ?" buttons {"Ye ...