当前位置:网站首页>[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));
}
}
边栏推荐
- Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
- php——laravel缓存cache
- Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
- [learning notes] segment tree selection
- Drawerlayout suppress sideslip display
- MongoDB 分片总结
- Mongodb replication (replica set) summary
- mysql 局域网内访问不到的问题
- Cinnamon Applet 入门
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
猜你喜欢
随机推荐
Solve the cache breakdown problem
MySQL error 28 and solution
一文读懂数仓中的pg_stat
Signal strength (RSSI) knowledge sorting
作战图鉴:12大场景详述容器安全建设要求
Introduce six open source protocols in detail (instructions for programmers)
2022-7-6 Leetcode 977.有序数组的平方
Shell batch file name (excluding extension) lowercase to uppercase
MongoDB命令汇总
xshell连接服务器把密钥登陆改为密码登陆
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
PHP - laravel cache
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
Deep understanding of array related problems in C language
Scripy tutorial classic practice [New Concept English]
记一次 .NET 某新能源系统 线程疯涨 分析
1. Deep copy 2. Call apply bind 3. For of in differences
C语言数组相关问题深度理解
QQ的药,腾讯的票








