[Solved] Alternating Code with Java, C++, Python

Alternating Code: It is IPL Season and the first league match of Dhilip’s favorite team, “Chennai Super Kings”. The CSK team is playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in action.

After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket, there is a letter-code that can be represented as a string of Latin letters.

Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code alternate. Otherwise, he believes that the team might lose. Please see note section for formal definition of alternating code.

You are given a ticket code. Please determine, whether CSK Team will win the match or not based on Dhilip’sconviction. Print “Yes” or “No” corresponding to the situation.

Note:
Two letters x, y where x != y are said to be alternating in a code, if code is of form “xyxyxy…“(case insensitive).

Input Format:
First and only line of the input contains a string S denoting the letter code on the ticket.

Output Format:
Output a single line containing “Yes” (without quotes) based on the conditions given and “No” otherwise.
Refer sample input and output for formatting specifications.

Sample Input1:
ABABAB
Sample Output1:
Yes

Sample Input2:
ABC
Sample Output2:
No

SampleInput3:
XYXYX
SampleOutput3:
Yes

Solution

import java.util.*;
class Main
{
    public static void main(String jr[])
    {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        str = str.toLowerCase();
        int f=1; 
        for( int  i=0;i<str.length()-2;i++)
        {
            //sauravhathi
            if(str.charAt(i)==str.charAt(i+2) && str.charAt(i)!=str.charAt(i+1))
            {
                continue;
            }
            else
            {  
                //sauravhathi
                f=0;
                break;
            }
        }    
            if(f==1)
            System.out.print("Yes");
            else
            System.out.print("No");
            
        
    }
}
#include <stdio.h>
#include <string.h>
int main()
{
    int i;
    char a[50];
    scanf("%s", a);
    //sauravhathi
    for (i = 0; (a[i] != '\0'); i++)
    {
        if (a[i] != a[i + 2] || a[i + 2] == '\0' || a[i] == a[i + 1])
            break;
    }
    if ((a[i + 2] == '\0'))
        printf("Yes");
    else
        printf("No");
    return 0;
}
s=input()
a=s[0]
b=s[1]
f=1
if a==b:
    f=0
else:
    c=a
    for i in s:
        if i!=c:
            f=0
#sauravhathi
            break
        if c==a:
            c=b
        else:
            c=a
if f==1:
#sauravhathi
    print('Yes')
else:
    print('No')

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *