In this HackerEarth Car Names, problem-solution Brian built his own car and was confused about what name he should keep for it. He asked Roman for help. Roman being his good friend, suggested a lot of names.
Brian only liked names that:
- Consisted of exactly three distinct characters, say C1, C2, and C3
- Satisfied the criteria that the string was of the form - C1n C2n C3n: This means, first C1 occurs n times, then C2 occurs n times and then C3 occurs n times. For example, xyz, ccaarr, mmmiiiaaa satisfy the criteria, but xyzw, aabbbcccc don't.
Given N names suggested by Roman, print "OK" if Brian likes the name and "Not OK" if he doesn't.


HackerEarth Car Names problem solution


HackerEarth Car Names problem solution.

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N; cin>>N;
    string s;
    while(N--) {
        cin>>s;
    int num1[1000]={0};
    int k=0;
    long long cnt=1;
    int num=0;
    for(int i=1;i<s.size();i++) {
        if(s[i]==s[i-1])cnt++;
        else {
            num1[k++]=cnt;
            if(k>3) break;
            num++; cnt=1;
        }
    }
    if(cnt>=1) {
        num1[k++]=cnt;
        num++;
    }
    if(num==3&&num1[0]==num1[1]&&num1[1]==num1[2] && s[0] != s[s.size() - 1])
      cout<<"OK"<<endl;
    else
      cout <<"Not OK"<<endl;
  }
}


Second solution

#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define FOR(i,lb,ub) for(i=lb;i<=ub;i++)
#define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
#define FORS(it,v) for(it=v.begin();it!=v.end();it++)
#define int long long
#define st_clk double st=clock();
#define end_clk double en=clock();
#define show_time cout<<"\tTIME="<<(en-st)/CLOCKS_PER_SEC<<endl;
int gcd(int a, int b) { return b?gcd(b,a%b):a; }
using namespace std;
void solve()
{
  int i,j,k,n;
  cin>>n;
  assert(n>=1 && n<=100);
  string s;
  FOR(i,0,n-1) {
    cin>>s;
    set<char> sc;
    int ok = true, sz = s.size();
    if (sz%3 != 0)
      ok = false;
    else {
      int j=0;
      sc.insert(s[j]);
      for (; j<sz/3-1; j++) {
        if (s[j]!=s[j+1])
          ok = false;
      }
      j++;
      sc.insert(s[j]);
      for (; j<2*sz/3 - 1; j++) {
        if (s[j]!=s[j+1])
          ok = false;
      }
      j++;
      sc.insert(s[j]);
      for (; j<sz-1; j++) {
        if (s[j]!=s[j+1])
          ok = false;
      }
    }
    if (sc.size()!=3)
      ok = false;
    if (!ok) {
      cout<<"Not OK\n";
    }
    else cout<<"OK\n";
  }
}
main()
{
    st_clk
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t,i,j;
    solve();
    
    return 0;
}