1342B. Binary Period - Codeforces Solution C++

  Problem Link : 1342B. Binary Period 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define mp make_pair



int main()
{
	int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        bool f=1;
        for(int i=1;i<s.length();i++)
            if(s[i]!=s[i-1])
            {
                f=0;
                break;
            }

        if(f)
        {
            cout<<s<<"\n";
            continue;
        }
        int n=s.length();
        string ans="";
        ans+=s[0];
        int i=1;
        while(i<n)
        {
            if(s[i]!=s[i-1])
                ans+=s[i++];
            else
            {
                if(s[i]=='0')
                    ans+="1";
                else
                    ans+="0";
                ans+=s[i++];
            }
        }
        cout<<ans<<"\n";
    }




}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!