You are viewing a single comment's thread. Return to all comments →
long substrCount(int n, string s) { long ans = 0; for (auto it = 0; it != string::npos; ) { auto mid = s.find_first_not_of(s[it], it); auto left = (mid == string::npos ? s.length() : mid) - it; ans += left*(left+1) / 2; if (mid != string::npos) { auto right = s.find_first_not_of(s[it], mid+1); right = ((right == string::npos) ? s.length() : right) - mid -1; ans += std::min(right, left); } it = mid; } return ans; }
Seems like cookies are disabled on this browser, please enable them to open this website
Special String Again
You are viewing a single comment's thread. Return to all comments →