当前位置:网站首页>[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));
}
}
边栏推荐
- Realbasicvsr test pictures and videos
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- 2022-7-6 Leetcode 977.有序数组的平方
- 分屏bug 小记
- JS缓动动画原理教学(超细节)
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- postgresql array类型,每一项拼接
- php——laravel缓存cache
- Navicat run SQL file import data incomplete or import failed
- Esp32 ① compilation environment
猜你喜欢
为租客提供帮助
Ogre introduction
Talk about pseudo sharing
Scrapy教程经典实战【新概念英语】
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
[Presto profile series] timeline use
C语言数组相关问题深度理解
Leecode3. Longest substring without repeated characters
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
分布式事务解决方案
随机推荐
干货|总结那些漏洞工具的联动使用
JNA learning notes 1: Concepts
Shell batch file name (excluding extension) lowercase to uppercase
为租客提供帮助
Custom thread pool rejection policy
ROS机器人更换新雷达需要重新配置哪些参数
User management summary of mongodb
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
Mongodb meets spark (for integration)
Server to server (S2S) event (adjust)
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
Some principles of mongodb optimization
MongoDB复制(副本集)总结
Summary of import, export, backup and recovery of mongodb
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Detr introduction
Realbasicvsr test pictures and videos
MySQL error 28 and solution
MongoDB命令汇总
LIS 最长上升子序列问题(动态规划、贪心+二分)