You are viewing a single comment's thread. Return to all comments →
C++ solution with two maps
m = store each words
pp = store all possible prefix except the word itself
the test case from https://www.hackerrank.com/challenges/one-week-preparation-kit-no-prefix-set/forum/comments/1338749 was super helpful.
void noPrefix(vector<string> words) { map<string, int> m, pp; string bad_str = ""; for (auto s: words) { string str = ""; for (int i = 0; i < s.size(); i++) { str += s[i]; if (i != s.size()-1) pp[str]++; if (m[str] > 0) { bad_str = s; break; } } m[s]++; if (pp[s] > 0) bad_str = s; if (bad_str != "") { cout << "BAD SET\n" << bad_str << "\n"; return; } } cout << "GOOD SET\n"; }
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++ solution with two maps
m = store each words
pp = store all possible prefix except the word itself
the test case from https://www.hackerrank.com/challenges/one-week-preparation-kit-no-prefix-set/forum/comments/1338749 was super helpful.