解题思路:
(1)排序+判断
class Solution {
public:
string helper(vector<string>& d,string &str) {
for(int i=0;i<d.size();i++) {
if(str.substr(0,d[i].length())==d[i]) {
return d[i];
}
}
return str;
}
string replaceWords(vector<string>& d, string s) {
sort(d.begin(),d.end());
int i=0;
string str="",temp="";
while(i<s.length()) {
if(isalpha(s[i])) {
str="";
while(i<s.length() && isalpha(s[i])) {
str+=s[i++];
}
temp+=helper(d,str);
} else temp+=s[i++];
}
return temp;
}
};