Strings In Binary

Given a binary string S, your work is to count the number of such substrings that start and end with 1, if there is no such combination print 0.

Example:

Input:  n = 4, s="1111"
Output: 6

Approach

C++

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

int stringInBinary(int nstring s)
{

    int i = 0j = n - 1;
    int cnt = 0k;
    while (i < j)
    {
        while (i < j && s[i] == '0')
            i++;
        while (i < j && s[j] == '0')
            j--;

        while (i < j)
        {
            if (s[j] == '1')
            {
                cnt++;
            }
            j--;
        }
        j = n - 1;
        i = i + 1;
    }
    return cnt;
}
int main()
{

    int n = 4;
    string s = "1111";

    cout << stringInBinary(ns<< "\n";

    return 0;
}


No comments:

Post a Comment