当前位置:网站首页>Force deduction solution summary 648 word replacement
Force deduction solution summary 648 word replacement
2022-07-22 19:32: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);
}
}
边栏推荐
- Leetcode daily question 2022/2/7-2022/2/13
- Unity: material download
- Fluent adjusts the drawing shape by dragging
- How can one page nest another page in thymeleaf? About page nesting, the tag tells you what you should know
- 30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
- Create objects using factory methods
- MySQL optimization enforces the use of indexes
- Leetcode daily question 2021/12/13-2021/12/19
- Introduction to date object
- Why is it necessary to rewrite the hashcode method when rewriting the requests method
猜你喜欢
Flutter 第一个程序Hello World!
Scenario practice | how to use rongyun super group to build a game community
Ps: how to call up auxiliary lines
Industrial router oilfield wireless monitoring
Grafana panel - about conversion
In his early 30s, he became a doctoral director of Fudan University. Chen Siming: I want to write both codes and poems
JS advanced - understanding of functions
Constructor
Swagger UI introduction and common notes
Scope and lifecycle of beans
随机推荐
Bean 的作用域和生命周期
"35 years old, I retired": This is the most reliable answer to the midlife crisis
Methods of wrapping classes and strings
JS advanced - basic data type and reference data type
2022 centos8 Yum image installation & Alibaba cloud MySQL 5.7 tutorial and problem solving
Leetcode daily question 2022/2/28-2022/3/6
MFC dialog program only runs a simple example of a single instance
Flutter 第一个程序Hello World!
浏览器逻辑漏洞合集
工业路由器油田无线监控
Batch check crawler
30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
场景实践 | 如何使用融云超级群构建游戏社区
Flutter's first program Hello world!
Rongyun handles political affairs: "small grid" can also achieve "big governance"
Execute function now
Huawei mobile phone locking application
The force deduction method summarizes the k-diff number pairs in the 532 array
Fluent adjusts the drawing shape by dragging
JS advanced - lexical scope