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. COGS 2334. [HZOI 2016]最小函数值

    时间限制:1 s   内存限制:128 MB [题目描述] 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Aix2+Bix+Ci(x∈N∗).给定这些Ai.Bi和Ci,请求出所有函数的所 ...

  2. 使用python模拟登陆百度

    #!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...

  3. initWithNibName/awakeFromNib/initWithCoder

    转自: http://leeyin.iteye.com/blog/1040362 每个ios开发者对loadView和viewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似 ...

  4. iOS的设计备忘录/资源集合(新手快速开发)

    iOS的设计备忘录 随着iOS7更新,风格走上扁平化,大部分iOS设计师及程序员都需要对自己的软件做相关调整,尺寸.Icon.UI等等,我在这里总结一下相关资料,以及提供一些关于iOS7设计素材. 一 ...

  5. iOS5 and iOS6都只支持横屏的方法

    If your app uses a UINavigationController, then you should subclass it and set the class in IB. You ...

  6. git 添加 ,密匙

    转载此处   https://blog.csdn.net/xiayiye5/article/details/79652296

  7. Salt Master报错:Minion did not return. [No response]

    在salt master端执行salt ‘*’ test.ping时,某一节点出现如下报错:Minion did not return. [No response] 登陆到这一节点查看minion的日 ...

  8. Encryption-基础:base64加解密

    环境:vc2003 .h /********** This library is free software; you can redistribute it and/or modify it und ...

  9. 单机简单搭建一个kafka集群(没有进行内核参数和JVM的调优)

    1.JDK安装 在我的部署单节点kafka的博客里有相关的方法.(https://www.cnblogs.com/ToBeExpert/p/9789486.html )zookeeper和kafka的 ...

  10. bzoj5138 [Usaco2017 Dec]Push a Box

    题目描述: bz luogu 题解: 暴力可以记录$AB$位置转移,这个时候状态是$n^4$的,无法接受. 考虑只记录$A$在$B$旁边时的状态,这个时候状态时$n^2$的. 所以说转移有两种,一种是 ...