Count of occurrences of a “1(0+)1†pattern in a string in C++


We are given a string str containing 0s,1s and other alphabets . It also contains patterns of the form “1(0+)1” where 0+ means any number (>0) of consecutive 0s. The goal is to find such patterns ( “1(0+)1” ) inside string str.

Let us understand with examples

Input − str = “abb010bb10111011”

Output − Count of occurrences of a “1(0+)1” pattern in a string are − 2

Explanation − The patterns inside str are highlighted: “abb010bb10111011”, “abb010bb10111011”

Input − str = “01001011001001100”

Output − Count of occurrences of a “1(0+)1” pattern in a string are − 4

Explanation − The patterns inside str are highlighted: “01001011001001100”, “01001011001001100”, “01001011001001100”, “01001011001001100”

Approach used in the below program is as follows

It can be seen that all patterns start and end with 1. We will mark the first 1 using a flag variable check=1 and skip all 0s.

For any other character (neither 0 nor 1 ) set check as 0.

If we find another 1 and flag check is 1 then see if the previous value is 0. If yes then increment count as previous 0 is in between two ones. For any non 0 or 1 set check again as 0.

  • Take input string as str.

  • Function Pattern_occurrences(string str, int length) takes the string and its length and returns the count of occurrences of a “1(0+)1” pattern in a string

  • Take the initial count as 0.

  • Take flag variable check as 0 initially.

  • Traverse str using for loop from index i=0 to i<length.

  • If current character str[i] is 1 and check is 0 then set check as 1 and continue.

  • If current character str[i] is 1 and check is 1. Then it is second 1. Check if previous value str[i-1] is 0. If yes then pattern found. Increment count.

  • If the current character is neither 0 nor 1 then it will never be part of the pattern. Set check as 0. Now next encountered 1 will be considered as the start of the next pattern (if exists).

  • At the end count will have a number of such patterns inside str.

  • Return count as result.

Example

 Live Demo

#include<iostream>
using namespace std;
int Pattern_occurrences(string str, int length){
   int count = 0;
   bool check = 0;
   for (int i = 0; i < length ; i++){
      if (str[i] == '1' && check == 1){
         if (str[i - 1] == '0'){
            count++;
         }
      }
      if (str[i] == '1' && check == 0){
         check = 1;
         continue;
      }
      if (str[i] != '0' && str[i] != '1'){
         check = 0;
      }
   }
   return count;
}
int main(){
   string str = "01010111011";
   int length = str.length();
   cout<<"Count of occurrences of a “1(0+)1” pattern in a string are: "<< Pattern_occurrences(str, length);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of occurrences of a “1(0+)1” pattern in a string are: 3

Updated on: 03-Dec-2020

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements