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.
Thanks for the logic and style, I'm sure many have learned from it!
Following the same logic described, I have the same solution written in C++ for anyone who wanted to see it in the language
Here it is:
long substrCount(int n, string s)
{
long count = 0;
vector<std::pair<char, int>> frequencies;
int i=0, j=0;
for ( i = 0; i < n; i++)
{
for( j = i+1; j < n; j++)
{
if (s[j] == s[i])
continue;
else
break;
}
frequencies.push_back(std::make_pair(s[i],j-i));
i = j-1;
}
for (i=0; i < frequencies.size(); i++)
count += (frequencies[i].second+1) * frequencies[i].second / 2;
for (i=1; i < frequencies.size()-1; i++)
{
if ( frequencies[i].second == 1 && frequencies[i-1].first == frequencies[i+1].first)
count += min(frequencies[i-1].second, frequencies[i+1].second);
}
return count;
Special String Again
You are viewing a single comment's thread. Return to all comments →
Thanks for the logic and style, I'm sure many have learned from it! Following the same logic described, I have the same solution written in C++ for anyone who wanted to see it in the language
Here it is:
long substrCount(int n, string s) {
}