当前位置:网站首页>力扣解法汇总648-单词替换
力扣解法汇总648-单词替换
2022-07-07 21:50:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根an,跟随着单词 other(其他),可以形成新的单词 another(另一个)。
现在,给定一个由许多词根组成的词典 dictionary 和一个用空格分隔单词形成的句子 sentence。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。
你需要输出替换之后的句子。
示例 1:
输入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
输出:"the cat was rat by the bat"
示例 2:
输入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
输出:"a a b c"
提示:
1 <= dictionary.length <= 1000
1 <= dictionary[i].length <= 100
dictionary[i] 仅由小写字母组成。
1 <= sentence.length <= 10^6
sentence 仅由小写字母和空格组成。
sentence 中单词的总量在范围 [1, 1000] 内。
sentence 中每个单词的长度在范围 [1, 1000] 内。
sentence 中单词之间由一个空格隔开。
sentence 没有前导或尾随空格。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/replace-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 使用Set存储dictionary中的字符,然后遍历sentence中的字符,每个字符都分别取其1,2,3,4,length的长度,看set中是否存在。
代码:
public class Solution648 {
public String replaceWords(List<String> dictionary, String sentence) {
Set<String> set = new HashSet<>(dictionary);
String[] strings = sentence.split(" ");
for (int i = 0; i < strings.length; i++) {
String str = strings[i];
for (int k = 1; k < str.length(); k++) {
String substring = str.substring(0, k);
if (set.contains(substring)) {
str = substring;
break;
}
}
strings[i] = str;
}
return String.join(" ", strings);
}
}边栏推荐
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- leetcode-520. 检测大写字母-js
- 每日一题——PAT乙级1002题
- Wechat forum exchange applet system graduation design completion (1) development outline
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- Unity 动态合并网格纹理
- Locate to the bottom [easy to understand]
- 14、 Two methods of database export and import
- ./ setup. Insufficient sh permission
- 微信论坛交流小程序系统毕业设计毕设(5)任务书
猜你喜欢

今日创见|企业促进创新的5大关键要素

微信论坛交流小程序系统毕业设计毕设(1)开发概要

Innovation today | five key elements for enterprises to promote innovation

Transform XL translation

Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020

14、 Two methods of database export and import

消息队列与快递柜之间妙不可言的关系
![Cause analysis and solution of too laggy page of [test interview questions]](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Cause analysis and solution of too laggy page of [test interview questions]

数据库每日一题---第22天:最后一次登录

七月第一周
随机推荐
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
JMeter interface automated test read case, execute and write back result
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
2021-01-12
为什么市场需要低代码?
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
2022 words for yourself
十四、数据库的导出和导入的两种方法
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
I wish you all the best and the year of the tiger
网络安全-安装CentOS
PCL . VTK files and Mutual conversion of PCD
Clean C disk
What does the model number of asemi rectifier bridge kbpc1510 represent
Database daily question --- day 22: last login
十三、系统优化
Bit operation
解决:信息中插入avi格式的视频时,提示“unsupported video format”
USB(十四)2022-04-12
Statistical method for anomaly detection