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

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

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

Example:

Input:  str="011011010"
Output: 3

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] == '1' && str[j] == '0')
        {
            while (str[i] == '1')
                i++;
            if (str[i] == '0')
            {
                count++;
            }
        }
        j = i;
        i++;
    }
    return count;
}
int main()
{
    string str = "011011010";

    cout << countPattern(str);

    return 0;
}


No comments:

Post a Comment