class Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
sort(dictionary.begin(), dictionary.end());
vector<string> word;
string temp;
for (auto x : sentence)
{
if (x != ' ')
temp.push_back(x);
else
{
word.push_back(temp);
temp.clear();
}
}
word.push_back(temp);
string ret;
for (auto x : word)
{
bool is_root = false;
for (auto y : dictionary)
{
if (x.find(y) == 0)
{
is_root = true;
ret += y;
break;
}
}
if (!is_root)
ret += x;
ret += " ";
}
ret.erase(ret.end() - 1);
return ret;
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)