Skip to content

Commit f975c9a

Browse files
committed
feat: add solutions to lc problem: No.0648
No.0648.Replace Words
1 parent 3f54348 commit f975c9a

File tree

8 files changed

+497
-384
lines changed

8 files changed

+497
-384
lines changed

solution/0600-0699/0648.Replace Words/README.md

Lines changed: 173 additions & 149 deletions
Large diffs are not rendered by default.

solution/0600-0699/0648.Replace Words/README_EN.md

Lines changed: 164 additions & 147 deletions
Large diffs are not rendered by default.

solution/0600-0699/0648.Replace Words/Solution.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,55 @@
11
class Trie {
2+
private:
3+
Trie* children[26];
4+
int ref;
5+
26
public:
3-
vector<Trie*> children;
4-
string v;
5-
Trie()
6-
: children(26)
7-
, v("") {}
7+
Trie(): ref(-1) {
8+
memset(children, 0, sizeof(children));
9+
}
810

9-
void insert(string word) {
11+
void insert(const string& w, int i) {
1012
Trie* node = this;
11-
for (char c : word) {
12-
c -= 'a';
13-
if (!node->children[c]) node->children[c] = new Trie();
14-
node = node->children[c];
13+
for (auto& c : w) {
14+
int idx = c - 'a';
15+
if (!node->children[idx]) {
16+
node->children[idx] = new Trie();
17+
}
18+
node = node->children[idx];
1519
}
16-
node->v = word;
20+
node->ref = i;
1721
}
1822

19-
string search(string word) {
23+
int search(const string& w) {
2024
Trie* node = this;
21-
for (char c : word) {
22-
c -= 'a';
23-
if (!node->children[c]) break;
24-
node = node->children[c];
25-
if (node->v != "") return node->v;
25+
for (auto& c : w) {
26+
int idx = c - 'a';
27+
if (!node->children[idx]) {
28+
return -1;
29+
}
30+
node = node->children[idx];
31+
if (node->ref != -1) {
32+
return node->ref;
33+
}
2634
}
27-
return word;
35+
return -1;
2836
}
2937
};
3038

3139
class Solution {
3240
public:
3341
string replaceWords(vector<string>& dictionary, string sentence) {
3442
Trie* trie = new Trie();
35-
for (auto& v : dictionary) trie->insert(v);
36-
string ans = "";
37-
istringstream is(sentence);
38-
vector<string> ss;
39-
string s;
40-
while (is >> s) ss.push_back(s);
41-
for (auto word : ss) ans += trie->search(word) + " ";
43+
for (int i = 0; i < dictionary.size(); ++i) {
44+
trie->insert(dictionary[i], i);
45+
}
46+
stringstream ss(sentence);
47+
string w;
48+
string ans;
49+
while (ss >> w) {
50+
int idx = trie->search(w);
51+
ans += (idx == -1 ? w : dictionary[idx]) + " ";
52+
}
4253
ans.pop_back();
4354
return ans;
4455
}
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
type Trie struct {
22
children [26]*Trie
3-
v string
3+
ref int
44
}
55

66
func newTrie() *Trie {
7-
return &Trie{}
7+
return &Trie{ref: -1}
88
}
9-
func (this *Trie) insert(word string) {
9+
10+
func (this *Trie) insert(w string, i int) {
1011
node := this
11-
for _, c := range word {
12-
c -= 'a'
13-
if node.children[c] == nil {
14-
node.children[c] = newTrie()
12+
for _, c := range w {
13+
idx := c - 'a'
14+
if node.children[idx] == nil {
15+
node.children[idx] = newTrie()
1516
}
16-
node = node.children[c]
17+
node = node.children[idx]
1718
}
18-
node.v = word
19+
node.ref = i
1920
}
2021

21-
func (this *Trie) search(word string) string {
22+
func (this *Trie) search(w string) int {
2223
node := this
23-
for _, c := range word {
24-
c -= 'a'
25-
if node.children[c] == nil {
26-
break
24+
for _, c := range w {
25+
idx := c - 'a'
26+
if node.children[idx] == nil {
27+
return -1
2728
}
28-
node = node.children[c]
29-
if node.v != "" {
30-
return node.v
29+
node = node.children[idx]
30+
if node.ref != -1 {
31+
return node.ref
3132
}
3233
}
33-
return word
34+
return -1
3435
}
3536

3637
func replaceWords(dictionary []string, sentence string) string {
3738
trie := newTrie()
38-
for _, v := range dictionary {
39-
trie.insert(v)
39+
for i, w := range dictionary {
40+
trie.insert(w, i)
4041
}
41-
var ans []string
42-
for _, v := range strings.Split(sentence, " ") {
43-
ans = append(ans, trie.search(v))
42+
ans := strings.Builder{}
43+
for _, w := range strings.Split(sentence, " ") {
44+
if idx := trie.search(w); idx != -1 {
45+
ans.WriteString(dictionary[idx])
46+
} else {
47+
ans.WriteString(w)
48+
}
49+
ans.WriteByte(' ')
4450
}
45-
return strings.Join(ans, " ")
51+
return ans.String()[:ans.Len()-1]
4652
}

solution/0600-0699/0648.Replace Words/Solution.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
class Trie {
2-
Trie[] children = new Trie[26];
3-
String v;
2+
private Trie[] children = new Trie[26];
3+
private int ref = -1;
44

5-
void insert(String word) {
5+
public void insert(String w, int i) {
66
Trie node = this;
7-
for (char c : word.toCharArray()) {
8-
c -= 'a';
9-
if (node.children[c] == null) {
10-
node.children[c] = new Trie();
7+
for (int j = 0; j < w.length(); ++j) {
8+
int idx = w.charAt(j) - 'a';
9+
if (node.children[idx] == null) {
10+
node.children[idx] = new Trie();
1111
}
12-
node = node.children[c];
12+
node = node.children[idx];
1313
}
14-
node.v = word;
14+
node.ref = i;
1515
}
1616

17-
String search(String word) {
17+
public int search(String w) {
1818
Trie node = this;
19-
for (char c : word.toCharArray()) {
20-
c -= 'a';
21-
if (node.children[c] == null) {
22-
return word;
19+
for (int j = 0; j < w.length(); ++j) {
20+
int idx = w.charAt(j) - 'a';
21+
if (node.children[idx] == null) {
22+
return -1;
2323
}
24-
node = node.children[c];
25-
if (node.v != null) {
26-
return node.v;
24+
node = node.children[idx];
25+
if (node.ref != -1) {
26+
return node.ref;
2727
}
2828
}
29-
return word;
29+
return -1;
3030
}
3131
}
3232

3333
class Solution {
3434
public String replaceWords(List<String> dictionary, String sentence) {
3535
Trie trie = new Trie();
36-
for (String v : dictionary) {
37-
trie.insert(v);
36+
for (int i = 0; i < dictionary.size(); ++i) {
37+
trie.insert(dictionary.get(i), i);
3838
}
3939
List<String> ans = new ArrayList<>();
40-
for (String v : sentence.split("\\s")) {
41-
ans.add(trie.search(v));
40+
for (String w : sentence.split("\\s")) {
41+
int idx = trie.search(w);
42+
ans.add(idx == -1 ? w : dictionary.get(idx));
4243
}
4344
return String.join(" ", ans);
4445
}
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
class Trie:
22
def __init__(self):
3-
self.children = [None] * 26
4-
self.v = None
3+
self.children: List[Trie | None] = [None] * 26
4+
self.ref: int = -1
55

6-
def insert(self, word):
6+
def insert(self, w: str, i: int):
77
node = self
8-
for c in word:
9-
idx = ord(c) - ord('a')
8+
for c in w:
9+
idx = ord(c) - ord("a")
1010
if node.children[idx] is None:
1111
node.children[idx] = Trie()
1212
node = node.children[idx]
13-
node.v = word
13+
node.ref = i
1414

15-
def search(self, word):
15+
def search(self, w: str) -> int:
1616
node = self
17-
for c in word:
18-
idx = ord(c) - ord('a')
17+
for c in w:
18+
idx = ord(c) - ord("a")
1919
if node.children[idx] is None:
20-
break
20+
return -1
2121
node = node.children[idx]
22-
if node.v:
23-
return node.v
24-
return word
22+
if node.ref != -1:
23+
return node.ref
24+
return -1
2525

2626

2727
class Solution:
2828
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
2929
trie = Trie()
30-
for v in dictionary:
31-
trie.insert(v)
32-
return ' '.join(trie.search(v) for v in sentence.split())
30+
for i, w in enumerate(dictionary):
31+
trie.insert(w, i)
32+
ans = []
33+
for w in sentence.split():
34+
idx = trie.search(w)
35+
ans.append(dictionary[idx] if idx != -1 else w)
36+
return " ".join(ans)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Trie {
2+
private children: Trie[];
3+
private ref: number;
4+
5+
constructor() {
6+
this.children = new Array<Trie>(26);
7+
this.ref = -1;
8+
}
9+
10+
public insert(w: string, i: number) {
11+
let node: Trie = this;
12+
for (const c of w) {
13+
const idx = c.charCodeAt(0) - 97;
14+
if (!node.children[idx]) {
15+
node.children[idx] = new Trie();
16+
}
17+
node = node.children[idx];
18+
}
19+
node.ref = i;
20+
}
21+
22+
public search(w: string): number {
23+
let node: Trie = this;
24+
for (const c of w) {
25+
const idx = c.charCodeAt(0) - 97;
26+
if (!node.children[idx]) {
27+
return -1;
28+
}
29+
node = node.children[idx];
30+
if (node.ref !== -1) {
31+
return node.ref;
32+
}
33+
}
34+
return -1;
35+
}
36+
}
37+
38+
function replaceWords(dictionary: string[], sentence: string): string {
39+
const trie = new Trie();
40+
for (let i = 0; i < dictionary.length; i++) {
41+
trie.insert(dictionary[i], i);
42+
}
43+
return sentence
44+
.split(' ')
45+
.map(w => {
46+
const idx = trie.search(w);
47+
return idx !== -1 ? dictionary[idx] : w;
48+
})
49+
.join(' ');
50+
}

solution/1300-1399/1335.Minimum Difficulty of a Job Schedule/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
考虑第 $j$ 天的工作安排,我们可以枚举第 $j$ 天完成的工作 $[k,..i]$,那么有状态转移方程:
7575

7676
$$
77-
f[i][j] = \min_{k \in [1,i]} \{f[k-1][j-1] + \max_{k \leq t \leq i} \{jobDifficulty[t]\}\}
77+
f[i][j] = \min_{k \in [1,i]} \{f[k-1][j-1] + \max_{k \leq t \leq i} \{jobDifficulty[t-1]\}\}
7878
$$
7979

8080
最终答案即为 $f[n][d]$。

0 commit comments

Comments
 (0)