[抄题]:

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"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么取单词前半截:.substring都忘了,实在是太弱了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

substring包左不包右,所以i需要 i <= word.length多加一位

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

取前半截用substring,而且右边界的i不包,要包就要加<=i的等号

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public String replaceWords(List<String> dict, String sentence) {
//corner case
if (dict == null || sentence.length() == 0)
return "";
//initialization: set
Set<String> set = new HashSet<String>(); //put all the words into set
for (int i = 0; i < dict.size(); i++) {
set.add(dict.get(i));
} //append the new result
StringBuilder sb = new StringBuilder();
String[] words = sentence.split("\\s+");
for (String word : words) {
String prefix = "";
for (int i = 1; i <= word.length(); i++) {
prefix = word.substring(0, i);
if (set.contains(prefix)) break;
}
sb.append(" " + prefix);
} //return
return sb.deleteCharAt(0).toString();
}
}

648. Replace Words 替换成为原来的单词的更多相关文章

  1. MySQL replace函数替换字符串语句的用法(mysql字符串替换)

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪. 最近在研究CMS,在数据转换的时候需要用 ...

  2. sql server replace的替换字符,replace的使用

    sql server replace的替换字符,replace的使用 select REPLACE(name,'张','') * from entity_5c7a578c05c7042958d9148 ...

  3. js replace 全局替换

    js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <script language="j ...

  4. js replace全部替换的方法

    1.JS replace()方法替换变量(可以对变量进行全文替换) string.replace(new RegExp(key,'g'),"b"); 2.封装 String.pro ...

  5. js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式

    js replace 全局替换   js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...

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

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

  7. [LeetCode] Replace Words 替换单词

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

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

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

  9. ORACLE 利用 REPLACE函数替换字段字符串

    REPLACE(string,s1,s2) string 希望被替换的字符或变量 s1 被替换的字符串 s2 要替换的字符串 SQL> select replace(he love you,he ...

随机推荐

  1. LeetCode - X of a Kind in a Deck of Cards

    In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...

  2. UEFI+GPT双硬盘安装Win10+Ubuntu16.04双系统

    转载请注明出处:http://www.cnblogs.com/willnote/p/6725594.html 安装环境 SSD+HDD双盘,Win10安装在SSD里,HDD分出来60G安装Ubuntu ...

  3. 3.1 MathType上标位置调整的两种方法

    具体操作步骤如下: 1.打开MathType窗口后在工作区域中编辑好公式. 2.调整上标位置有两种方法: (1)选中要调整的上标,按下“Ctrl+↑,Ctrl+↓,Ctrl+←,Ctrl+→”进行调整 ...

  4. Logistic Loss的简单讨论

    首先应该知道Logistic Loss和Crossing Entropy Loss本质上是一回事. 所以所谓的SoftMaxLoss就是一般二分类LogisitcLoss的推广.之所以在网络中采取这种 ...

  5. c# 数据存储过程

    什么是存储过程? 用某百科的话来说就是一堆为了完成某一功能或者某些功能的SQL语句的集合,而数据库则会将这些存储过程的方法存储到数据库中去. 优点: 1.可重用并且效率高:存储过程经过一次编译后不需要 ...

  6. MySQL:System.Data.Entity ,MySqlCommand, MySqlParameter and "LIKE" %

    Introduction Using GetSqlStringCommand with a text comparative, with LIKE, in ADO.NET and the MySQLP ...

  7. c语言fork 多进程

    fork函数的作用 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两 ...

  8. JavaScript图形库

    做前端少不了画图,html5 的 canva 很强大,可是如果所有需求都自己画图,恐怕会被 leader 炒掉.记录一下我用过的几个 2D 和 3D 的js图形库,各种功能.我就不分 2D 和 3D ...

  9. 将SQL for xml path('')中转义的字符正常显示

    在工作中出现的发送邮件的时候:因为邮件内容中有链接,并且多个拼接在一起的,于是用了for xml path().       但是,这样显示出来的链接时会将路径中的<,>,&符号转 ...

  10. Android入门(一) IDEA上创建Android应用之helloworld

    Android入门(一) IDEA上创建Android应用之helloworld 首先看运行结果: 一.准备工作 下载安装IntelliJ IDEA :我这里用的是2018.2.7 下载安装Genym ...