-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path648_Replace_Words_(Medium).cpp
48 lines (48 loc) · 1.69 KB
/
648_Replace_Words_(Medium).cpp
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 Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
// Simple Parsing through the string:
// TC: O(N), SC: O(N) (unordered_set)
unordered_set<string> words;
for(auto a : dictionary){
words.insert(a);
}
string result = "";
int ind = 0;
int size = sentence.size();
string temp = "";
while(ind < size && sentence[ind] != ' '){ // To get the first root word
if(words.find(temp) != words.end()){
break;
}
temp += sentence[ind];
++ind;
}
while(ind < size && sentence[ind] != ' '){ // If first word is bigger than root word, then go to the next word
++ind;
}
++ind; // Disregard the space
result += temp;
while(ind < size){
temp = "";
while(ind < size && sentence[ind] != ' '){ // Get the root word
if(words.find(temp) != words.end()){
break;
}
temp += sentence[ind];
++ind;
}
while(ind < size && sentence[ind] != ' '){ // If still characters left, then go to the next space
++ind;
}
if(ind < size && sentence[ind] == ' '){ // If space found, then go to the next root word
++ind;
}
if(temp != ""){ // Root found, add it to the result
result += ' ';
result += temp;
}
}
return result;
}
};