You are viewing a single comment's thread. Return to all comments →
C++ without map:
string isValid(string s) { if (s.size() == 1) return "YES";
sort(s.begin(), s.end()); vector<int> cvector; int counter = 1; for (int i = 0; i < s.size(); i++) { if (i >= 1) { if (s.at(i) == s.at(i-1)) { counter++; } else { cvector.push_back(counter); counter = 1; } } } cvector.push_back(counter); sort(cvector.begin(), cvector.end()); for (int i = 0; i< cvector.size(); i++) { fprintf(stderr,"%d ", cvector.at(i)); if (i == cvector.size() - 1) { fprintf(stderr,"\n"); } } int firstStrike = 0; // take care for case 1 2 2 2 2 2 if (cvector.size() > 2 && (cvector.at(1) - cvector.at(0) >= 1)) { cvector.erase(cvector.begin()); firstStrike++; } for (int i = 1; i < cvector.size(); i++) { static int refer = cvector.at(0); if (cvector.at(i) - refer > 1) { return "NO"; } else { if (cvector.at(i) != refer) { firstStrike++; } } } if (firstStrike > 1) return "NO"; return "YES";
}
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →
C++ without map:
string isValid(string s) { if (s.size() == 1) return "YES";
}