550A Codeforces - Two Substrings


550A Codeforces - Two Substrings



Solution in C++

#include <bits/stdc++.h>

using namespace std;

int n,sol,AB,BA,ABA,flag=-1;

string inp;

int main() {

    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>inp;
    n=inp.size();
    for(int i=1;i<n;i++)
    {
        if(inp[i]=='A' && inp[i-1]=='B')
        {
            BA++;
            if(flag==i-1)ABA++,i++;
            flag=i;
        }
        else if(inp[i]=='B' && inp[i-1]=='A')
        {
            AB++;
            if(flag==i-1)ABA++,i++;
            flag=i;
        }
    }
    AB-=ABA;
    BA-=ABA;
    if(ABA>1 || (AB && ABA) || (BA && ABA) || (AB && BA)) cout<<"YES\n";
    else cout<<"NO";
    return 0;
}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق