当前位置:网站首页>力扣解法汇总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);
}
}边栏推荐
- 十四、数据库的导出和导入的两种方法
- When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
- Handling file exceptions
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- 数据库每日一题---第22天:最后一次登录
- 十三、系统优化
- 数字藏品加速出圈,MarsNFT助力多元化文旅经济!
- Network security - joint query injection
- One question per day - pat grade B 1002 questions
- 成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
猜你喜欢

What does the model number of asemi rectifier bridge kbpc1510 represent

Software test classification

微生物健康網,如何恢複微生物群落

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

Binary tree

PCL . VTK files and Mutual conversion of PCD

Database daily question --- day 22: last login

Wechat forum exchange applet system graduation design completion (4) opening report

高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏

成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
随机推荐
网络安全-对操作系统进行信息查询
Bit operation
2022 words for yourself
USB (十八)2022-04-17
微生物健康网,如何恢复微生物群落
iNFTnews | NFT技术的广泛应用及其存在的问题
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
ArcGIS:矢量要素相同字段属性融合的两种方法
QT graphicsview graphical view usage summary with flow chart development case prototype
648. 单词替换
Understand the session, cookie and token at one time, and the interview questions are all finalized
网络安全-beef
网络安全-钓鱼
智慧社区和智慧城市之间有什么异同
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
网络安全-永恒之蓝
海内外技术人们“看”音视频技术的未来
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
知识点滴 - PCB制造工艺流程
Brush question 4