We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidnoPrefix(vector<string>words){unordered_set<string_view>prefix_set,word_set;// iterate over all words, using references is crucial for unordered_set<string_view> to workfor(constauto&w:words){autoword_sv=string_view(w);// check if current word w is a prefix of any previous wordif(prefix_set.count(word_sv)){printf("BAD SET\n%s\n",w.c_str());return;}// iterate over all possible non-empty prefixes of current word wfor(autosize=1u;size<=w.size();size++){autoword_prefix_sv=word_sv.substr(0,size);// check if any previous word is a prefix of current word wif(word_set.count(word_prefix_sv)){printf("BAD SET\n%s\n",w.c_str());return;}// update prefix setprefix_set.insert(word_prefix_sv);}// update word_setword_set.insert(word_sv);}printf("GOOD SET\n");}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
C++17 code for your reference: