当前位置:网站首页>[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));
}
}
边栏推荐
- Split screen bug notes
- 靠卖概念上市,认养一头牛能走多远?
- Getting started with MySQL
- Deep understanding of array related problems in C language
- [1] ROS2基础知识-操作命令总结版
- .net core 关于redis的pipeline以及事务
- JNA learning notes 1: Concepts
- Pcap learning notes II: pcap4j source code Notes
- [Presto profile series] timeline use
- PAcP learning note 1: programming with pcap
猜你喜欢
Final review notes of single chip microcomputer principle
ESP32 ① 编译环境
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
Scripy tutorial classic practice [New Concept English]
Digital IC Design SPI
Cmake learning and use notes (1)
10 pictures open the door of CPU cache consistency
[learning notes] agc010
Milkdown control icon
OSI 七层模型
随机推荐
Custom thread pool rejection policy
MongoDB优化的几点原则
118. 杨辉三角
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
Write it down once Net a new energy system thread surge analysis
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Esp32 construction engineering add components
Cinnamon taskbar speed
Esp32 series column
Storage principle inside mongodb
分布式事务解决方案
Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
clion mingw64中文乱码
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
单片机原理期末复习笔记
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
[Presto profile series] timeline use
JS determines whether an object is empty
得物客服热线的演进之路
High end for 8 years, how is Yadi now?