当前位置:网站首页>Force deduction solution summary 648 word replacement
Force deduction solution summary 648 word replacement
2022-07-07 23:21:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
In English , We have one called Root (root) The concept of , You can add other words after the root to form another longer word —— We call it Inheritance words (successor). for example , Root an, Follow the word other( other ), Can form new words another( the other one ).
Now? , Given a dictionary consisting of many roots dictionary And a sentence formed by separating words with spaces sentence. You need to replace all the inherited words in the sentence with roots . If an inherited word has many roots that can form it , Replace it with the shortest root .
You need to output the replaced sentences .
Example 1:
Input :dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output :"the cat was rat by the bat"
Example 2:
Input :dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output :"a a b c"
Tips :
1 <= dictionary.length <= 1000
1 <= dictionary[i].length <= 100
dictionary[i] It's only made up of lowercase letters .
1 <= sentence.length <= 10^6
sentence Only lowercase letters and spaces .
sentence The total number of words in the range [1, 1000] Inside .
sentence The length of each word in the range [1, 1000] Inside .
sentence Words in are separated by a space .
sentence No leading or trailing spaces .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/replace-words
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Use Set Storage dictionary The characters in , Then traverse sentence The characters in , Each character takes its own 1,2,3,4,length The length of , see set Whether there is .
Code :
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);
}
}
边栏推荐
- Network security -beef
- 树后台数据存储(採用webmethod)[通俗易懂]
- USB(十六)2022-04-28
- LDO voltage stabilizing chip - internal block diagram and selection parameters
- JS get the key and value of the object
- 微信论坛交流小程序系统毕业设计毕设(1)开发概要
- Binary tree
- How to generate unique file names
- Guessing game (read data from file)
- 解决:信息中插入avi格式的视频时,提示“unsupported video format”
猜你喜欢
In the field of software engineering, we have been doing scientific research for ten years!
Explain
微信论坛交流小程序系统毕业设计毕设(4)开题报告
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Mysql索引优化实战一
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
Technology at home and abroad people "see" the future of audio and video technology
深入理解Mysql锁与事务隔离级别
[microservices SCG] gateway integration Sentinel
随机推荐
ArcGIS:矢量要素相同字段属性融合的两种方法
Grid
GEE(三):计算两个波段间的相关系数与相应的p值
系统架构设计师备考经验分享:论文出题方向
1. Sum of two numbers
Network security - phishing
网络安全-永恒之蓝
RE1 attack and defense world reverse
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
经纬度PLT文件格式说明
CXF call reports an error. Could not find conduct initiator for address:
树后台数据存储(採用webmethod)[通俗易懂]
Network security -burpsuit
STL标准模板库(Standard Template Library)一周学习总结
13、 System optimization
网络安全-钓鱼
MATLAB signal processing [Q & A essays · 2]
网络安全-beef
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
sql 数据库执行问题