-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path648.cpp
More file actions
22 lines (22 loc) · 799 Bytes
/
Copy path648.cpp
File metadata and controls
22 lines (22 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
string replaceWords(vector<string> &dict, string sentence) {
unordered_set<string> d(dict.begin(), dict.end());
sentence += ' ';
int start = 0, end = sentence.find(' ');
while (end != -1) {
string str = sentence.substr(start, end - start);
for (int i = 0; i < str.size(); ++i) {
if (d.find(str.substr(0, i + 1)) != d.end()) {
sentence.replace(sentence.begin() + start, sentence.begin() + end, str.substr(0, i + 1));
end = start + i + 1;
break;
}
}
start = end + 1;
end = sentence.find(' ', start);
}
sentence.erase(sentence.end() - 1);
return sentence;
}
};