A word is grouped if, for each letter in the word, all occurrences of that letter form exactly one consecutive sequence. In other words, no two equal letters are separated by one or more letters that are different.
Given a vector<string>
return the number of grouped words.
For example :
{"ab", "aa", "aca", "ba", "bb"}
return 4.
Here, "aca" is not a grouped word.
My quick and dirty solution :
int howMany(vector <string> words) {
int ans = 0;
for (int i = 0; i < words.size(); i++) {
bool grouped = true;
for (int j = 0; j < words[i].size()-1; j++)
if (words[i][j] != words[i][j+1])
for (int k = j+1; k < words[i].size(); k++)
if (words[i][j] == words[i][k])
grouped = false;
if (grouped) ans++;
}
return ans;
}
I want a better algorithm for the same problem.