Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 648. Replace Words
題目:

解答:
class Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
stringstream ss(sentence);
string s;
string ans;
unordered_set<string> st(dictionary.begin(), dictionary.end());
while (ss >> s) {
string prefix;
string tmp = s;
for (char c : s) {
prefix += c;
if (st.count(prefix)) {
tmp = prefix;
break;
}
}
ans += tmp + " ";
}
ans.pop_back();
return ans;
}
};