当前位置:网站首页>[daily training] 648 Word replacement
[daily training] 648 Word replacement
2022-07-07 13:37:00 【Puppet__】
subject
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 <= 106
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 .
Code
package dayLeetCode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class dayleetcode648 {
// simulation Pay attention to set Otherwise, the time limit will be exceeded
public String replaceWords(List<String> dictionary, String sentence) {
Set<String> set = new HashSet<>();
for (String str : dictionary){
set.add(str);
}
String[] s = sentence.split(" ");
// StringBuffer ansStr = new StringBuffer();
for (int j = 0; j < s.length; j++){
// From the beginning of each word Be afraid to break the word root with or without giving
String str = s[j];
for (int i = 0; i < str.length(); i++){
if (set.contains(str.substring(0, i))){
s[j] = str.substring(0, i);
break;
}
}
// if (j == s.length - 1){
// ansStr.append(s[j]);
// }
// else{
// ansStr.append(s[j] + " ");
// }
}
return String.join(" ", s);
}
public static void main(String[] args) {
dayleetcode648 obj = new dayleetcode648();
List<String> dict = new ArrayList<>();
dict.add("cat");
dict.add("bat");
dict.add("rat");
String str = "the cattle was rattled by the battery";
System.out.println(obj.replaceWords(dict, str));
}
}
边栏推荐
- Deep understanding of array related problems in C language
- PAcP learning note 3: pcap method description
- [etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- 靠卖概念上市,认养一头牛能走多远?
- Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
- Write it down once Net a new energy system thread surge analysis
- 提升树莓派性能的方法
- Navicat运行sql文件导入数据不全或导入失败
- Move base parameter analysis and experience summary
- Ikvm of toolbox Net project new progress
猜你喜欢
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
cmake 学习使用笔记(一)
Deep understanding of array related problems in C language
提升树莓派性能的方法
LeetCode简单题分享(20)
Getting started with cinnamon applet
OSI seven layer model
Show the mathematical formula in El table
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
.net core 关于redis的pipeline以及事务
随机推荐
Cinnamon taskbar speed
记一次 .NET 某新能源系统 线程疯涨 分析
Scrapy教程经典实战【新概念英语】
How to make join run faster?
如何让join跑得更快?
High end for 8 years, how is Yadi now?
华为镜像地址
Esp32 construction engineering add components
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
566. 重塑矩阵
MongoDB的用户管理总结
clion mingw64中文乱码
Vscade editor esp32 header file wavy line does not jump completely solved
MongoDB复制(副本集)总结
DID登陆-MetaMask
为租客提供帮助
Data refresh of recyclerview
How far can it go to adopt a cow by selling the concept to the market?
室内ROS机器人导航调试记录(膨胀半径的选取经验)
Move base parameter analysis and experience summary