Patterns of 1(0+)1 in the given string

Write a program to find all the patterns of 1(0+)1 in the given string.

1(0+)1: There should be at least one 0' between the two 1's.

Example:

Input:  str="010011010"
Output: 2

Approach

C++

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

int countPattern(string str)
{
    int count = 0;

    int j = 0;

    int i = 1n = str.size();

    while (i < n)
    {
        if (str[i] == '0' && str[j] == '1')
        {
            while (str[i] == '0')
                i++;
            if (str[i] == '1')
            {
                count++;
            }
        }
        j = i;
        i++;
    }
    return count;
}
int main()
{
    string str = "010011010";

    cout << countPattern(str);

    return 0;
}


No comments:

Post a Comment