-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path648.ReplaceWords.cs
152 lines (131 loc) · 4.77 KB
/
648.ReplaceWords.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 648. Replace Words
// 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 derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".
// Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.
// Return the sentence after the 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"
// Constraints:
// 1 <= dictionary.length <= 1000
// 1 <= dictionary[i].length <= 100
// dictionary[i] consists of only lower-case letters.
// 1 <= sentence.length <= 106
// 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]
// Every two consecutive words in sentence will be separated by exactly one space.
// sentence does not have leading or trailing spaces.
//Trie
public class Solution {
Trie root = new Trie();
public string ReplaceWords(IList<string> dictionary, string sentence) {
InsertPrefix(dictionary);
return Convert(sentence);
}
public void InsertPrefix(IList<string> dictionary) {
foreach (var word in dictionary) {
Trie node = root;
foreach (var ch in word) {
if (!node.Contains(ch))
node.Insert(ch);
node = node.child[ch - 'a'];
}
node.SetEnd();
}
}
public string Convert(string sentence) {
List<string> words = sentence.Split(" ").ToList();
StringBuilder result = new StringBuilder();
foreach (var word in words) {
StringBuilder prefix = new StringBuilder();
Trie node = root;
bool found = false;
foreach (var ch in word) {
if (node.Contains(ch)) {
prefix.Append(ch);
node = node.child[ch - 'a'];
if (node.IsEnd()) {
found = true;
break;
}
} else {
break;
}
}
if (found) {
result.Append(prefix);
} else {
result.Append(word);
}
result.Append(" ");
}
// Remove the extra space at the end
if (result.Length > 0) {
result.Length--;
}
return result.ToString();
}
public class Trie {
public Trie[] child;
bool isEnd;
public Trie() {
child = new Trie[26];
isEnd = false;
}
public void Insert(char ch) {
child[ch - 'a'] = new Trie();
}
public bool Contains(char ch) {
return child[ch - 'a'] != null;
}
public bool IsEnd() {
return isEnd;
}
public void SetEnd() {
isEnd = true;
}
}
}
//HashSet
public class Solution {
public string ReplaceWords(IList<string> dictionary, string sentence) {
StringBuilder word = new StringBuilder("");
HashSet<string> wordSet = new HashSet<string>(dictionary);
StringBuilder result = new StringBuilder("");;
for(int c = 0; c < sentence.Length; c++){
if(sentence[c] != ' '){
word.Append(sentence[c]);
if(wordSet.Contains(word.ToString())){
while(c+1 < sentence.Length && sentence[c+1] != ' '){
c++;
}
}
}
else{
if(word.Length > 0){
result.Append(word);
word = new StringBuilder("");
}
result.Append(sentence[c]);
}
}
if(word.Length > 0){
result.Append(word);
}
return result.ToString();
}
}
//Linq
public class Solution {
public string ReplaceWords(IList<string> dictionary, string sentence) {
return string.Join(" ",
sentence.Split()
.Select(w => dictionary
.Where(d => d.Length <= w.Length && w[..d.Length].Equals(d))
.MinBy(x => x.Length) ?? w)
);
}
}