当前位置:网站首页>力扣解法汇总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);
}
}
边栏推荐
- Clean C disk
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- Statistical method for anomaly detection
- Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
- 30讲 线性代数 第五讲 特征值与特征向量
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- Wechat forum exchange applet system graduation design completion (7) Interim inspection report
- ArcGIS: two methods of attribute fusion of the same field of vector elements
- leetcode-520. 检测大写字母-js
- Bit operation
猜你喜欢
Are the microorganisms in the intestines the same as those on the skin?
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Database daily question --- day 22: last login
PMP project management exam pass Formula-1
【微服务|SCG】gateway整合sentinel
Unity and webgl love each other
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
DTC社群运营怎么做?
ArcGIS:矢量要素相同字段属性融合的两种方法
随机推荐
七月第一周
[language programming] exe virus code example
I wish you all the best and the year of the tiger
Redhat下安装fedora
oc 可变參数传递
Handling file exceptions
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
iNFTnews | NFT技术的广泛应用及其存在的问题
二叉树(Binary Tree)
Advantages and disadvantages of rest ful API
Mitsubishi PLC SLmP (MC) protocol
小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
JMeter interface automated test read case, execute and write back result
opencv scalar传入三个参数只能显示黑白灰问题解决
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
STL标准模板库(Standard Template Library)一周学习总结
网络安全-联合查询注入
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
PCL . VTK files and Mutual conversion of PCD