当前位置:网站首页>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);
}
}边栏推荐
- Ros2 topic (03): the difference between ros1 and ros2 [02]
- MATLAB signal processing [Q & A essays · 2]
- Introduction to redis and jedis and redis things
- 在软件工程领域,搞科研的这十年!
- Redhat下安装fedora
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- Binary tree
- Two kinds of curves in embedded audio development
- Why does the market need low code?
- 解决:信息中插入avi格式的视频时,提示“unsupported video format”
猜你喜欢

Inftnews | web5 vs Web3: the future is a process, not a destination

UE4_UE5全景相机

Wechat forum exchange applet system graduation design (5) assignment

Introduction to redis and jedis and redis things

JS get the key and value of the object

【编译原理】词法分析设计实现

When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK

LDO稳压芯片-内部框图及选型参数

Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid

PMP project management exam pass Formula-1
随机推荐
Install Fedora under RedHat
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
leetcode-520. 检测大写字母-js
LDO稳压芯片-内部框图及选型参数
UE4_UE5结合罗技手柄(F710)使用记录
FreeLink开源呼叫中心设计思想
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
Network security - joint query injection
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
网络安全-永恒之蓝
海内外技术人们“看”音视频技术的未来
十四、数据库的导出和导入的两种方法
智慧社区和智慧城市之间有什么异同
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
Unity3D学习笔记6——GPU实例化(1)
智慧社區和智慧城市之間有什麼异同
在软件工程领域,搞科研的这十年!
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
Guessing game (read data from file)
kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)