Description
In English, we have a concept called root, which can be followed by some other word to form another longer word - let’s call this word successor. For example, when the root “an” is followed by the successor word “other”, we can form a new word “another”.
Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root that has the shortest length.
Return the sentence after the replacement.
Examples
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”
Constraints:
1 <= dictionary.length <= 1000
1 <= dictionary[i].length <= 100
dictionary[i] consists of only lower-case letters.
1 <= sentence.length <= 10^6
sentence consists of only lower-case letters and spaces.
The number of words in sentence is in the range [1, 1000]
The length of each word in sentence is in the range [1, 1000]
Each two consecutive words in sentence will be separated by exactly one space.
sentence does not have leading or trailing spaces.
思路
因为是按照分类刷的题,所以提前就知道要用前缀树的方法做
题目给出了一个前缀字典和一个字符串,对字符串中的每个单词进行判断,如果符合字典中的前缀,就用前缀进行替代,否则依旧为他本身
需要注意的是,如果字典中的两个前缀,其中一个前缀为另一个前缀的前缀(如a, aa,a为aa的前缀),则只会识别a而不会识别aa
因此完整的流程分为两步
- 第一步是根据字典构建前缀树
- 第二步是对字符串中的每个单词,与前缀树进行判断,这里每执行一个单词都要判断一下是否到了end(也就是字典中是否存在该单词),并进行替换/保留操作
以example4为例
首先是构建字典的前缀树,构建的流程就不赘述了,本次和208的构建有点区别,是用了TreeNode [26]数组和isEnd标识作为一个TreeNode,具体可以看代码,这里直接给出前缀树的结构
然后对字符串进行匹配[the cattle was rattled by the battery]
不难发现虽然cattle能匹配上catt,但是由于每个字母都会判定是否为end,因此到cat的时候就直接输出cat了
代码
class TreeNode {
boolean isEnd;
TreeNode[] children = new TreeNode[26];
public TreeNode(){
for(TreeNode child: children){
child = null;
}
isEnd = false;
}
public void setEnd(){
isEnd = true;
}
public boolean contains(int num){
return children[num] != null;
}
public void addChild(int num){
children[num] = new TreeNode();
}
}
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
TreeNode root = new TreeNode();
for(String dic: dictionary){
TreeNode node = root;
for(int i = 0; i < dic.length(); i++){
int currentNum = dic.charAt(i) - 'a';
if(node.contains(currentNum)){
if (node.isEnd)
break;
}
else{
node.addChild(currentNum);
}
node = node.children[currentNum];
}
node.setEnd();
}
String[] sentences = sentence.split(" ");
String answer = "";
for(String s: sentences){
String temp = "";
TreeNode node = root;
boolean flag = false;
for(int i = 0; i < s.length(); i++){
int currentNum = s.charAt(i) - 'a';
temp = temp + s.charAt(i);
if(node.contains(currentNum)){
if (node.children[currentNum].isEnd){
flag = true;
break;
}
node = node.children[currentNum];
}
else{
break;
}
}
if(flag)
answer = answer + temp + " ";
else
answer = answer + s + " ";
}
return answer.strip();
}
}