class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
string rep;
if(sentence.size()==0)
return rep;
if(dict.size()==0)
return sentence;
int start=0,end=0;
while(end<sentence.size()){
if(sentence[end]!=' '){
end++;
}
else{
string str=sentence.substr(start,end-start);
string res=str;
for(int i=0;i<dict.size();i++){
if(dict[i].size()>str.size())
continue;
string temp=str.substr(0,dict[i].size());
if(temp==dict[i]){
if(temp.size()<res.size())
res=temp;
}
}
rep += res;
rep += ' ';
start=end+1;
end++;
}
}
string str=sentence.substr(start,end-start);
string res=str;
for(int i=0;i<dict.size();i++){
if(dict[i].size()>str.size())
continue;
string temp=str.substr(0,dict[i].size());
if(temp==dict[i]){
if(temp.size()<res.size())
res=temp;
}
}
rep += res;
return rep;
}
};