Distinct Binary Strings Solution || Codechef Starters - 27

       

Distinct Binary Strings Solution 

Codechef Starters - 27
Problem Code : BINSTRING

Solution :

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;

void solve()
{
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll ans = 1;
    char prev = s[0];
    for (ll i = 1; i < n; i++)
    {
        if (s[i] == prev)
        {
            prev = s[i];
        }
        else
        {
            prev = s[i];
            ans++;
        }
    }
    cout << ans << "\n";
}

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Comments

Post a Comment