当前位置:网站首页>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);
}
}
边栏推荐
- JMeter-接口自动化测试读取用例,执行并结果回写
- leetcode-520. 检测大写字母-js
- V20变频器手自动切换(就地远程切换)的具体方法示例
- LeeCode -- 6. Zigzag transformation
- Advantages and disadvantages of rest ful API
- Wechat forum exchange applet system graduation design (3) background function
- 云原生数据仓库AnalyticDB MySQL版用户手册
- ROS2专题(03):ROS1和ROS2的区别【01】
- Coreseek: the second step is index building and testing
- Wechat forum exchange applet system graduation design completion (7) Interim inspection report
猜你喜欢
微信论坛交流小程序系统毕业设计毕设(1)开发概要
UE4_UE5结合罗技手柄(F710)使用记录
Ros2 topic (03): the difference between ros1 and ros2 [01]
ArcGIS: two methods of attribute fusion of the same field of vector elements
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
海内外技术人们“看”音视频技术的未来
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
LDO稳压芯片-内部框图及选型参数
随机推荐
网络安全-钓鱼
三问TDM
Inftnews | web5 vs Web3: the future is a process, not a destination
Advantages and disadvantages of rest ful API
13、 System optimization
Wechat forum exchange applet system graduation design completion (4) opening report
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
力扣解法汇总648-单词替换
网络安全-beef
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
微信论坛交流小程序系统毕业设计毕设(1)开发概要
Introduction to redis and jedis and redis things
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
Install a new version of idea. Double click it to open it
LDO voltage stabilizing chip - internal block diagram and selection parameters
kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
U盘拷贝东西时,报错卷错误,请运行chkdsk
Coreseek: the second step is index building and testing