
题目描述
在英语中,我们有一个叫做 词根(root) 的概念,可以词根 后面 添加其他一些词组成另一个较长的单词——我们称这个词为 衍生词 (derivative)。例如,词根 help,跟随着 继承词 "ful",可以形成新的单词 "helpful"。
现在,给定一个由许多 词根 组成的词典 dictionary 和一个用空格分隔单词形成的句子 sentence。你需要将句子中的所有 衍生词 用 词根 替换掉。如果 衍生词 有许多可以形成它的 词根,则用 最短 的 词根 替换它。
你需要输出替换之后的句子。
示例 1:
输入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
输出:"the cat was rat by the bat"
示例 2:
输入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
输出:"a a b c"
提示:
1 <= dictionary.length <= 1000 1 <= dictionary[i].length <= 100 dictionary[i] 仅由小写字母组成。 1 <= sentence.length <= 106 sentence 仅由小写字母和空格组成。 sentence 中单词的总量在范围 [1, 1000] 内。 sentence 中每个单词的长度在范围 [1, 1000] 内。 sentence 中单词之间由一个空格隔开。 sentence 没有前导或尾随空格。
解法
方法一:前缀树
我们可以使用前缀树来存储词典中的所有词根。定义前缀树节点类 \(\text{Trie}\),其中包含一个长度为 \(26\) 的数组 \(\text{children}\) 来存储子节点,以及一个布尔变量 \(\text{is\_end}\) 来标记是否为一个完整的词根。
对于每个词根,我们将其插入前缀树中。对于句子中的每个单词,我们在前缀树中搜索其最短的词根,如果找到了,则替换该单词,否则保持不变。
时间复杂度 \(O(n \times |w| + L)\),空间复杂度 \(O(n \times |w|)\),其中 \(n\) 和 \(|w|\) 分别是词典中词根的数量和平均长度,而 \(L\) 是句子中单词的总长度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | class Trie:
def __init__(self):
self.children = [None] * 26
self.is_end = False
def insert(self, w: str) -> None:
node = self
for c in w:
idx = ord(c) - ord("a")
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True
def search(self, w: str) -> str:
node = self
for i, c in enumerate(w, 1):
idx = ord(c) - ord("a")
if node.children[idx] is None:
return w
node = node.children[idx]
if node.is_end:
return w[:i]
return w
class Solution:
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
trie = Trie()
for w in dictionary:
trie.insert(w)
return " ".join(trie.search(w) for w in sentence.split())
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 | class Trie {
Trie[] children = new Trie[26];
boolean isEnd = false;
void insert(String w) {
Trie node = this;
for (char c : w.toCharArray()) {
int idx = c - 'a';
if (node.children[idx] == null) {
node.children[idx] = new Trie();
}
node = node.children[idx];
}
node.isEnd = true;
}
String search(String w) {
Trie node = this;
for (int i = 0; i < w.length(); i++) {
int idx = w.charAt(i) - 'a';
if (node.children[idx] == null) {
return w;
}
node = node.children[idx];
if (node.isEnd) {
return w.substring(0, i + 1);
}
}
return w;
}
}
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
Trie trie = new Trie();
for (String w : dictionary) {
trie.insert(w);
}
String[] words = sentence.split(" ");
for (int i = 0; i < words.length; i++) {
words[i] = trie.search(words[i]);
}
return String.join(" ", words);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 | class Trie {
public:
Trie* children[26]{};
bool isEnd = false;
void insert(const string& w) {
Trie* node = this;
for (char c : w) {
int idx = c - 'a';
if (!node->children[idx]) {
node->children[idx] = new Trie();
}
node = node->children[idx];
}
node->isEnd = true;
}
string search(const string& w) {
Trie* node = this;
for (int i = 0; i < w.size(); ++i) {
int idx = w[i] - 'a';
if (!node->children[idx]) {
return w;
}
node = node->children[idx];
if (node->isEnd) {
return w.substr(0, i + 1);
}
}
return w;
}
};
class Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
Trie trie;
for (auto& w : dictionary) {
trie.insert(w);
}
stringstream ss(sentence);
string word, res;
while (ss >> word) {
if (!res.empty()) res += " ";
res += trie.search(word);
}
return res;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 | type Trie struct {
children [26]*Trie
isEnd bool
}
func (t *Trie) insert(w string) {
node := t
for _, c := range w {
idx := c - 'a'
if node.children[idx] == nil {
node.children[idx] = &Trie{}
}
node = node.children[idx]
}
node.isEnd = true
}
func (t *Trie) search(w string) string {
node := t
for i, c := range w {
idx := c - 'a'
if node.children[idx] == nil {
return w
}
node = node.children[idx]
if node.isEnd {
return w[:i+1]
}
}
return w
}
func replaceWords(dictionary []string, sentence string) string {
trie := &Trie{}
for _, w := range dictionary {
trie.insert(w)
}
words := strings.Split(sentence, " ")
for i, w := range words {
words[i] = trie.search(w)
}
return strings.Join(words, " ")
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 | class Trie {
children: Array<Trie | null>;
isEnd: boolean;
constructor() {
this.children = new Array(26).fill(null);
this.isEnd = false;
}
insert(w: string): void {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
node.children[idx] = new Trie();
}
node = node.children[idx]!;
}
node.isEnd = true;
}
search(w: string): string {
let node: Trie = this;
for (let i = 0; i < w.length; i++) {
const idx = w.charCodeAt(i) - 97;
if (!node.children[idx]) {
return w;
}
node = node.children[idx]!;
if (node.isEnd) {
return w.slice(0, i + 1);
}
}
return w;
}
}
function replaceWords(dictionary: string[], sentence: string): string {
const trie = new Trie();
for (const w of dictionary) {
trie.insert(w);
}
return sentence
.split(' ')
.map(w => trie.search(w))
.join(' ');
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 | struct Trie {
children: Vec<Option<Box<Trie>>>,
is_end: bool,
}
impl Trie {
fn new() -> Self {
Self {
children: (0..26).map(|_| None).collect(),
is_end: false,
}
}
fn insert(&mut self, w: String) {
let mut node = self;
for c in w.chars() {
let idx = (c as u8 - b'a') as usize;
node = node.children[idx].get_or_insert(Box::new(Trie::new()));
}
node.is_end = true;
}
fn search(&self, w: &str) -> String {
let mut node = self;
for (i, c) in w.chars().enumerate() {
let idx = (c as u8 - b'a') as usize;
if node.children[idx].is_none() {
return w.to_string();
}
node = node.children[idx].as_ref().unwrap();
if node.is_end {
return w[..i + 1].to_string();
}
}
w.to_string()
}
}
impl Solution {
pub fn replace_words(dictionary: Vec<String>, sentence: String) -> String {
let mut trie = Trie::new();
for w in dictionary {
trie.insert(w);
}
sentence
.split_whitespace()
.map(|w| trie.search(w))
.collect::<Vec<_>>()
.join(" ")
}
}
|