×

CAKEWALK

PREREQUISITES:

Basics of looping, conditional statements in any language.

PROBLEM:

Given a string $s$, find whether exactly two different letters alternate in it or not. e.g. if $s$ = "ABABAB", then the answer will be yes or if $s$ = "ABC", then the answer would be no, if $s$ = "AA" answer would be no because the alternating characters should be different as stated in the problem.

EXPLANATION:

Given a string, first check the starting two characters, if they are the same (character is in consecutive position instead of alternating) then clearly the answer is NO. Otherwise we just have to check whether the first two characters (i.e. $s[0]$ and $s[1]$) are occurring in even and odd positions respectively or not.

Pseudo Code

 if(S[0]==S[1]) return false;
for(i=0; i+2 < N; i++)
if(S[i]!= S[i+2]) return false;
return true;


The above code first checks the first two character and if they are different then check the whole string whether it follows the alternating character condition. The time complexity of the algorithm is $\mathcal{O}(|s|)$ as we are iterating over all the characters of string s only once.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

1951916
accept rate: 0%

19.8k350498541

 1 Your pseudo code is working wrong, cuz if u use a string for example: ABABABAAAAAAAAAA, then it gives true, which is false. answered 02 Mar '17, 18:42 12 accept rate: 0%
 0 Consider the test case when the string is : "ABA" What should be the output of this string? what does the alternate mean?:- "Pair of characters" should be in alternate for or "Single character" should be in alternate form. answered 23 Jun '16, 23:51 1 accept rate: 0% it will be true ABABABA format is correct if ABA is incorrect then strlen(s)%2!=0 cases directly gives NO but it;s not the case if strlen is 1 then it's true so u can understand it with this case. but odd length of string should be in test cases (07 Sep '16, 15:59)
 0 import java.util.*; class test { public void check() { Scanner sc=new Scanner(System.in); System.out.println("Enter the Stirng pattern"); String s=sc.nextLine(); int l=s.length(); int i=2; boolen flag=True; char c=s.charAt(0); char cc=s.charAt(1); if(c!=cc) { while(flag!=False) { while(i

include<string.h>

int main() { int t,i,len; char str[101]; scanf("%d",&t); while(t--) { int flag=0; scanf("%s",str); len=strlen(str); for(i=2;i<len;i++) { if(i%2==0) { if(str[i]==str[0]) { flag=1; } else { flag=0;break; } } else if(i%2!=0) { if(str[i]==str[1]) flag=1; else { flag=0;break; } } } if(flag==1) printf("YES\n"); else if(flag==0) printf("NO\n"); } return 0; }

1
accept rate: 0%

 0 Hi, for this exercise, i'm running the below code. But it keeps saying wrong answer on all the subtasks. I couldn't figure out why. Please review. t = int(input()) for i in range(t): str = input() if str[0] == str[1]: print('No') else: x = ''.join(set(str[::2])) y = ''.join(set(str[1::2])) if len(x) > 1 or len(y) > 1: print('No') else: print('Yes') answered 12 Sep '16, 17:47 1●1 accept rate: 0%
 0 how to solve it ?? help answered 11 Oct '16, 20:23 0★arjarjun 1 accept rate: 0%
 0 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class CodeChef1 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Integer t = Integer.valueOf(reader.readLine()); while (t > 0) { String s = reader.readLine(); if(s.charAt(0)==s.charAt(2) && s.charAt(1)==s.charAt(3)) System.out.println("YES"); else System.out.println("NO"); t--; } }  } what is wrong in my code its showing runtime error.why? answered 20 Oct '16, 00:19 1 accept rate: 0%
 0 I am getting wrong answer for this plz someone tell whats the error int main() { string temp; long int k,x; cin>>k; while(k--){ long int count = 0,count1=0; cin >> temp; x=temp.length(); for(int i=0;i
 0 @shivamsst You're just checking only the first 3 characters of the string to make your decision. If the string, say s, is "ABABCDCD" your code will print "YES" since s[0] == s[2] ("A" == "A") and also s[0] != s[1] (since "A" != "B") but you did not check the rest of string if it matches the criteria! You should follow the pseudo-code given in the editorial. It is clean and compact. You will understand it easily. answered 18 Dec '16, 10:32 371●2 accept rate: 23%

include<bits stdc++.h="">

using namespace std; int main() { int t; scanf("%d",&t); while(t--) { string s; cin>>s; char a,b; int l=s.size(),f=0; for(int i=0;i<l;i++) { if(i==0) a=s[i]; if(i==1 && s[i]!=a) b=s[i]; else if(s[i]==a && i==1) { f=1; break; } if((i%2!=0 && s[i]!=b)||(i%2==0 && s[i]!=a)) { f=1; break; } } if(f==0) cout<<"YES"; else cout<<"NO"; cout<<endl; } return 0; }

5★salman_1
1
accept rate: 0%

what is wrong in this code not understanding

include <stdio.h>

int main() { int t,i,c; char s[110]; scanf("%d",&t); while(t--) { scanf("%s",s); c=0; int l=strlen(s); if(l==2&&s[0]!=s[1]) {printf("YES\n");} for(i=0;i<l-2&&l>2;i++) { if(s[i]==s[i+2]) c=1; else{ c=0; } if(c==0) printf("NO\n"); break; }

    if(c==1)
{
printf("YES\n");
}


} }

1
accept rate: 0%

 0 Umm there could be a better approach but it isn't working and I really don't know why... My logic was to extract a string from index 0 to 1 and then replace its occurrences in the original string by "", now if the length of resultant string is empty o/p "YES" else "NO"... here's my snippet for this : if(s.length()%2!=0) return "NO"; else { String temp = s.substring(0,2); s = s.replaceAll(temp, ""); if(s.length()==0) return "YES"; else return "NO"; } but it shows wrong o/p pls help answered 17 Jun '17, 19:46 1 accept rate: 0% Your method is actually better. The only problem is- your code ouputs "NO" for all cases where the length is odd numbered. This is not correct. Example- "ABA" answer should be YES, but your code gives no. Same goes with ABABA, which should actually give "YES". (26 Jun '17, 16:40)
 0 should ABA and ABABAC give YES? answered 17 Jul '17, 14:07 20●5 accept rate: 0%
 0 should ABA and ABABAC give YES? answered 17 Jul '17, 14:07 20●5 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,652
×54

question asked: 16 Apr '15, 16:01

question was seen: 4,799 times

last updated: 26 Jun '17, 16:40