SRM 432 DIV2 GroupedWordChecker

0

Problem Statement

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. For example, the words “ccazzzzbb” and “code” are grouped, while “aabbbccb” and “topcoder” are not.

You are given several words as a vector <string>. Return how many of them are grouped.
Definition

Class:
GroupedWordChecker
Method:
howMany
Parameters:
vector <string>
Returns:
int
Method signature:
int howMany(vector <string> words)
(be sure your method is public)
Constraints

words will contain between 1 and 50 elements, inclusive.

Each element of words will contain between 1 and 50 characters, inclusive.

Each element of words will contain only lowercase letters (‘a’ – ‘z’).

All elements of words will be distinct.
Examples
0)
{“ccazzzzbb”, “code”, “aabbbccb”, “topcoder”}
Returns: 2
As mentioned in the problem statement, the first two words are grouped.
1)
{“ab”, “aa”, “aca”, “ba”, “bb”}
Returns: 4
“aca” is not a grouped word.
2)
{“happy”, “new”, “year”}
Returns: 3

3)
{“yzyzy”, “zyzyz”}
Returns: 0

4)
{“z”}
Returns: 1

My code is not efficient.But since its 250 pts problem.Just speed matters 🙂

#include <map>
#include <string>
#include <vector>
using namespace std;
class GroupedWordChecker{
public:
int howMany(vector<string> words){
string candidate;
map<char,int> m;
int c=0,len=words.size(),flag=0;
for(int i=0;i<len;i++){
flag=0;
candidate.assign(words[i]);
int l=candidate.size();
// m[candidate[0]]=0;
for(int j=0;j<l;j++){
m[candidate[j]]=-1;
}
for(int j=1;j<l;j++){
if(candidate[j]!=candidate[j-1]){
m[candidate[j-1]]=j-1;
if(m[candidate[j]]!=-1){
flag=1;
}
}
}
if(!flag)
c++;
}
return c;
}
};
view raw SRM432.cpp hosted with ❤ by GitHub