struct comp {
bool operator() (const std::string& a, const std::string& b) const{
if(a.size() == b.size()) {
return a<b;
}
return a.size() > b.size();
}
};
string longestWord(vector<string>& words) {
set<string, comp> st;
for(auto s: words) st.insert(s);
for(auto &s: st) {
int i = s.size()-1;
for(; i>=1; i--) {
if(!st.count(s.substr(0,i))) break;
}
if(i==0) return s;
}
return "";
}
No comments:
Post a Comment