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 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:

  1. The input will only have lower-case letters.
  2. 1 <= dict words number <= 1000
  3. 1 <= sentence words number <= 1000
  4. 1 <= root length <= 100
  5. 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的更多相关文章

  1. 648. Replace Words 替换成为原来的单词

    [抄题]: In English, we have a concept called root, which can be followed by some other words to form a ...

  2. LC 648. Replace Words

    In English, we have a concept called root, which can be followed by some other words to form another ...

  3. 【LeetCode】648. Replace Words 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...

  4. LeetCode 648. Replace Words (单词替换)

    题目标签:HashMap 题目给了我们一个array 的 root, 让我们把sentence 里面得每一个word 去掉它得 successor. 把每一个root 存入hash set,然后遍历s ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. 算法与数据结构基础 - 字典树(Trie)

    Trie基础 Trie字典树又叫前缀树(prefix tree),用以较快速地进行单词或前缀查询,Trie节点结构如下: //208. Implement Trie (Prefix Tree)clas ...

  7. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. 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 ...

随机推荐

  1. Python3实现自动备份

    需求 将重要文件备份到指定目录,存档文件名称为“当前日期.zip”. 前提 1) Windows系统 2) Python 3以上版本 旗舰版 #!usr/bin/python # -*- coding ...

  2. 使用 Azure 创建网络文件系统

    本快速入门介绍了如何使用 Azure 文件存储实现网络文件共享.在本教程中完成的所有操作均符合 1 元试用条件. 本快速入门介绍了如何使用 Azure 文件存储实现网络文件共享.在本教程中完成的所有操 ...

  3. Stream.iterate方法与UnaryOperator

    前提:本人在翻看<Java核心技术II>的时候在p17的时候发现一段代码不是很明白.不知道为什么就输出了1,2,3,4,5,6,7,8,9,10,...也不知道n-n.add(BigInt ...

  4. ubuntu 16.0安装 hadoop2.8.3

    环境:ubuntu 16.0 需要软件:jdk ssh https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 2.8.3 安装 jdk并 ...

  5. 最常见的 5 个导致节点重新启动、驱逐或 CRS 意外重启的问题 (文档 ID 1524455.1)

    适用于: Oracle Database - Enterprise Edition - 版本 10.1.0.2 到 11.2.0.3 [发行版 10.1 到 11.2]本文档所含信息适用于所有平台 用 ...

  6. 欧拉回路/通路 Codeforces Round #288 (Div. 2)

    http://codeforces.com/contest/508/problem/D 以上是题目链接 题目大意 给n个字符串看能不能链接在一起 因为 三个三个分割 所以字符串 如abc ab作为起点 ...

  7. keras中的shape/input_shape

    在keras中,数据是以张量的形式表示的,张量的形状称之为shape,表示从最外层向量逐步到达最底层向量的降维解包过程.“维”的也叫“阶”,形状指的是维度数和每维的大小.比如,一个一阶的张量[1,2, ...

  8. WPF中单选框RadioButton

    单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...

  9. [BZOJ4899]:记忆的轮廓(概率DP)

    题目传送门 题目描述: 通往贤者之塔的路上,有许多的危机. 我们可以把这个地形看做是一颗树,根节点编号为1,目标节点编号为n,其中1-n的简单路径上,编号依次递增, 在[1,n]中,一共有n个节点.我 ...

  10. 欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍

    1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x* ...