【LeetCode】648.Replace Words(Medium)解题报告
来源:互联网 发布:法律面前人人平等 知乎 编辑:程序博客网 时间:2024/05/16 08:05
【LeetCode】648.Replace Words(Medium)解题报告
题目地址:https://leetcode.com/problems/replace-words/description/
题目描述:
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.
Note:1. The input will only have lower-case letters.2. 1 <= dict words number <= 10003. 1 <= sentence words number <= 10004. 1 <= root length <= 1005. 1 <= sentence words length <= 1000
Input: dict = ["cat", "bat", "rat"]sentence = "the cattle was rattled by the battery"Output: "the cat was rat by the bat"
Solution:
class Solution { public String replaceWords(List<String> dict, String sentence) { Set<String> set = new HashSet<>(dict); StringBuilder sb = new StringBuilder(); String[] words = sentence.split(" "); for(int i=0;i<words.length;i++){ for(int j=0;j<words[i].length();j++){ String root = words[i].substring(0,j); if(set.contains(root)){ words[i]=root; } } sb.append(words[i]+" "); } return sb.substring(0,sb.length()-1); }}
Date:2017年12月12日
阅读全文
0 0
- 【LeetCode】648.Replace Words(Medium)解题报告
- Leetcode 648. Replace Words 替换词语 解题报告
- Leetcode--648. Replace Words
- leetcode 648. Replace Words
- leetcode-648. Replace Words
- leetcode 648. Replace Words
- leetcode 648. Replace Words
- LeetCode解题报告 53. Maximum Subarray [medium]
- LeetCode解题报告 338. Counting Bits [medium]
- LeetCode解题报告 413. Arithmetic Slices [medium]
- LeetCode解题报告 343. Integer Break [medium]
- LeetCode解题报告 392. Is Subsequence [medium]
- LeetCode解题报告 120. Triangle [medium]
- LeetCode解题报告 279. Perfect Squares [medium]
- LeetCode解题报告 394. Decode String [medium]
- LeetCode解题报告 55. Jump Game [medium]
- LeetCode解题报告 322. Coin Change [medium]
- LeetCode解题报告 399. Evaluate Division [medium]
- etcd 启用 https
- C# 占位符的使用
- 百度上传插件 WebUploader初始使用
- Spring MVC rest 09 火推
- 900AFind Extra One
- 【LeetCode】648.Replace Words(Medium)解题报告
- scala数据结构之Set和Map
- MVC AOP 10火推
- 最新高清IT职业技能图谱:机器学习、架构师、大数据、前端、运维,全套免费下载
- 图像分割中各个指标的计算方法
- 斐波那契数列的三种解法及时间复杂度
- java--设计模式
- 应用TF-Slim快速实现迁移学习
- Android基础知识二(应用程序框架)