BUS TICKETS

Description:

Every day, Mike goes to his job by a bus, where he buys a ticket. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters. Mike believes that the day will be successful in case exactly two different letters in the code alternate. Otherwise, he believes that the day will be unlucky. Please see note section for formal definition of alternating code. You are given a ticket code. Please determine, whether the day will be successful for Mike or not. Print ""YES"" or ""NO"" (without quotes) corresponding to the situation. Test Case 1 Input (stdin) 2 ABAB AB Expected Output YES YES Test Case 2 Input (stdin) 1 ACAC Expected Output YES

Program :


#include <stdio.h>

int main()

{

  int i,k,test,t;

  char a[100],second,first;

  scanf("%d",&t);

  while(t--)

  {

    scanf("%s",a);

    k=1;

    test=1;

    first=a[0];

    second=a[1];

    if(first!=second)

    {

      for(i=2;a[i]!='\0';i++)

      {

        if(k)

        {

          if(a[i]!=first)

          {

            test=0;

            break;

          }

          k=0;

        }

          else

          {

            if(a[i]!=second)

            {

              test=0;

              break;

            }

            k=1;

          }

        }

        if(test==0)

          printf("NO\n");

        else

          printf("YES\n");

      }

      else

        printf("NO\n");

    }

    return 0;

}