Monday, June 29, 2015

Car Names

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.
Input:
First line contains a single positive integer N - the number of names.
N lines follow - each line contains a name.
Output:
For each name, Print "OK" if the name satisfies the criteria, else print "Not OK", on a new line.

Sample Input


2
bbbrrriii
brian

Sample Output


OK
Not OK




import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception 
{
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String line = br.readLine();
         int N = Integer.parseInt(line);
         for (int i = 0; i < N; i++) 
        {
               String name=br.readLine();
              display(name);
        }
       
}
        public static void display(String s)
       {
List<Character> chArr = new ArrayList<Character>();
List<Integer> nubArr = new ArrayList<Integer>();
char prev = ' ';
int count = 0;
int loopCount = 0;
for(char c : s.toCharArray())
                  {
if (c !=  prev)
                          {
if (prev != ' ') 
                                {
chArr.add(prev);
nubArr.add(count);
}
prev = c;
count = 1;
}
                         else
                         {
count++;
}
loopCount++;
if (s.length() == loopCount)
                        {
if (c ==  prev)
                              {
chArr.add(prev);
nubArr.add(count);
}
                              else 
                              {
nubArr.set(nubArr.size()-1,count);
}

}
}
int size = chArr.size();
if (size == 3)  
                           {
int tem = 0;
boolean flag = true;
for(int c : nubArr)
                                       {
if (tem == 0)
                                             {
tem = c;
}
                                            else if ( tem  != c) 
                                            {
flag = false;
}

}

if (flag) 
                                        {
System.out.println("OK");
}
                                        else
                                         {
System.out.println("Not OK");
}
}
                                 else
                                 {
System.out.println("Not OK");
}
}
}

0 comments:

Post a Comment