当前位置:网站首页>力扣解法汇总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);
}
}边栏推荐
- 位运算(Bit Operation)
- Guessing game (read data from file)
- Wechat forum exchange applet system graduation design completion (7) Interim inspection report
- Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
- 数据库每日一题---第22天:最后一次登录
- Network security - phishing
- 消息队列与快递柜之间妙不可言的关系
- 2021-01-11
- USB(十四)2022-04-12
- 三菱PLC slmp(mc)协议
猜你喜欢

JMeter interface automated test read case, execute and write back result

Gbu1510-asemi power supply special 15A rectifier bridge gbu1510

Innovation today | five key elements for enterprises to promote innovation

leetcode-520. 检测大写字母-js

Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!

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

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

Personal statement of testers from Shuangfei large factory: is education important for testers?

【刷题记录】3. 无重复字符的最长子串

Talk about DART's null safety feature
随机推荐
QT graphicsview graphical view usage summary with flow chart development case prototype
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
PMP项目管理考试过关口诀-1
Txt file virus
[network] Introduction to C language
网络安全-安装CentOS
Talk about DART's null safety feature
Wechat forum exchange applet system graduation design (5) assignment
STL标准模板库(Standard Template Library)一周学习总结
二叉树(Binary Tree)
USB(十六)2022-04-28
kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
Dynamics 365 查找字段过滤
十三、系统优化
About idea cannot find or load the main class
USB (十七)2022-04-15
Cause analysis and solution of too laggy page of [test interview questions]
Personal statement of testers from Shuangfei large factory: is education important for testers?