You are on page 1of 3

S2 BTech

C Programming

Code-Chef Submission

Name: ANANDHA KRISHNAN. R

Roll No: AM.EN.U4ECE17109

QUESTION: 2: TICKETS5

BEGINNER

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.

//ANANDHA KRISHNAN.R

#include<stdio.h>
#include<string.h>
int main()
{
int t;

scanf("%d",&t);

while(t--)
{

char str[100],c1,c2;
int i, check =1;

scanf("%s",str);

c1 = str[0];
c2 = str[1];

if(c1 != c2)
{
for(i=2;i<strlen(str);i+=2)
{

if(str[i] != c1)
{
check = 0;
}
}
for(i=3;i<strlen(str);i+=2)
{
if(str[i] != c2)
{
check = 0;
}
}
}
if(check == 1 && c1!=c2)
printf("YES\n");

else if(check == 0 || c1 == c2)


printf("NO\n");
}
return 0;
}
APPROACH:
I took t as the number of test cases and iterating the while loop and
taking all elements as input then I am checking whether the codes
are alternate .If alternate it prints yes, otherwise no.

You might also like