当前位置:网站首页>Sword finger offer II 063. replacement word (medium prefix tree string)
Sword finger offer II 063. replacement word (medium prefix tree string)
2022-07-28 22:25:00 【Calm in the wind and rain】
The finger of the sword Offer II 063. Replace words
In English , There's one called Root (root) The concept of , It can be followed by other words 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 of many roots and a sentence , You need to replace all 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 sentence after replacement .
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”
Example 3:
Input :dictionary = [“a”, “aa”, “aaa”, “aaaa”], sentence = “a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa”
Output :“a a a a a a a a bbb baba a”
Example 4:
Input :dictionary = [“catt”,“cat”,“bat”,“rat”], sentence = “the cattle was rattled by the battery”
Output :“the cat was rat by the bat”
Example 5:
Input :dictionary = [“ac”,“ab”], sentence = “it is abnormal that this solution is accepted”
Output :“it is ab that this solution is ac”
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.com/problems/UhWRSj
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
The application of prefix tree .
This problem is obviously to use prefix tree , The last question is similar to , First, build the data structure of prefix tree , And then dictionary The string of is added to the prefix tree , Yes sentence Judge the words in , If the prefix of a word is in the prefix tree, the prefix string is returned , Replace current word , Otherwise, it returns empty , The current word does not change . It should be noted that , Used String Of join() Method , The first parameter of this method is the delimiter , The following parameters can be multiple strings separated by commas , It can also be the string array of this topic or the dynamic string array , Method is a string that connects all strings and adds a separator between them . Such as :
String.join(“-”,“You”,“are”,“Awesome”) return "You-are-Awesome"
Answer key (Java)
class Solution {
static class TrieNode {
TrieNode children[];
boolean isWord;
public TrieNode() {
children = new TrieNode[26];
}
}
public String replaceWords(List<String> dictionary, String sentence) {
TrieNode root = buildTrie(dictionary);
String[] wordArray = sentence.split(" ");
for (int i = 0; i < wordArray.length; i++) {
String prefix = findPrefix(root, wordArray[i]);
if (!"".equals(prefix)) {
wordArray[i] = prefix;
}
}
return String.join(" ", wordArray);
}
private TrieNode buildTrie(List<String> dictionary) {
TrieNode root = new TrieNode();
for (String word : dictionary) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
if (node.children[ch - 'a'] == null) {
node.children[ch - 'a'] = new TrieNode();
}
node = node.children[ch - 'a'];
}
node.isWord = true;
}
return root;
}
private String findPrefix(TrieNode root, String word) {
TrieNode node = root;
StringBuilder sb = new StringBuilder();
for (char ch : word.toCharArray()) {
if (node.isWord || node.children[ch - 'a'] == null) {
break;
}
sb.append(ch);
node = node.children[ch - 'a'];
}
return node.isWord ? sb.toString() : "";
}
}
边栏推荐
- Chapter 7: drawing rotating cubes
- 成立不到一年!MIT衍生量子计算公司完成900万美元融资
- Differences of display values
- tutorial/detailed_workflow.ipynb 量化金融Qlib库
- Win11 how to open software notification
- Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
- Principle of object. Prototype. ToString. Call()
- What is time complexity
- HCIP(13)
- What does GPRS network mean
猜你喜欢

Hcip experiment (12)

Day3 classification management of Ruiji takeout project

成立不到一年!MIT衍生量子计算公司完成900万美元融资

CDN工作原理

阿里云CDN实践

MySQL built-in functions

DHCP和PPPoE协议以及抓包分析

Lvs+keepalived high availability deployment practical application

Aimbetter insight into your database, DPM and APM solutions
![[machine learning] naive Bayesian classification of text -- Classification of people's names and countries](/img/95/1f5b0a17a00da5473180667ccc33e2.png)
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
随机推荐
[Ruiji takeout project]day4 - dish management
Overall introduction of Ruiji takeout project
[Ruiji takeout] day05 package management business development
腾讯云数据库负责人借了一亿元炒股?知情人士:金额不实
lotus 1.16.0 延长扇区过期时间
【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
display 各值的区别
From Web3 to web2.5, is it backward or another way?
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
HCIP第七次实验
openresty 请求鉴权
04. Default value of toref
Win11怎么打开软件通知
为什么 0.1 + 0.2 不等于0.3?如何解决这个问题?
Object.prototype.toString.call()的原理
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
What is the difference between inline elements and block level elements? Semantic function
内网渗透学习(三)域横向移动——计划任务
HCIP(9)
罗克韦尔AB PLC RSLogix数字量IO模块基本介绍