Count of non-overlapping sub-strings β€œ101” and β€œ010” in the given binary string


A binary string is a sequence of characters comprising of 0’s and 1’s only. A binary string can’t contain any other character. Some of the examples of binary strings are β€œ0000”,”01010” or β€œ11111”

A substring of a given string is a sequence of characters taken in order. A string may contain multiple substrings. Non-overlapping strings are the strings where the indices of any two found substrings should not collide with each other. This implies that for any two substrings substr1[a..b] and substr2[c..d], either of the following, bd should be true.

The problem statement involves the computation of the substrings of a given string matching with substrings β€œ101” or β€œ010”.

Sample Example

Example 1 - str : β€œ1010100010101”

Output - 4

Explanation -The following substrings satisfy the criteria according to the problem statement βˆ’

str[0:2] - 101
str[3:5] - 010
str[7:9] - 010
str[10:12] - 101

An important point to note in the above example is that , once str[0:2] - 101 is counted, the substring str[1:3] = 010 (which is also a valid substring according to the problem) cannot be taken, since this would lead to overlapping of the indices [1:2] in the found strings.

The approach used to solve this problem is based on the sliding window concept, where the size of the window is kept to be equivalent to the substring length to be computed.

Algorithm

  • Step 1 βˆ’ A binary string is taken as an input string.

  • Step 2 βˆ’ The input string is converted to an array of characters.

  • Step 3 βˆ’ A counter is maintained to keep a track on the number of non-overlapping strings β€œ010” and β€œ101”, named by cnt.

  • Step 4 βˆ’ Iterate over the length of the string until the second last character is reached.

  • Step 5 βˆ’ Keep a window of three, to match the characters at the three indices with the given substrings.

  • Step 6 βˆ’ Check the current index character is equivalent to β€œ0”, the next with β€œ1” and the subsequent next with β€œ0” or if the current index character is equivalent to β€œ1”, the next with β€œ0” and the subsequent next with β€œ1”.

  • Step 7 βˆ’ If any of the case holds, increment the counter by 3, to get to next window.

  • Step 8 βˆ’ The counter is also incremented by 1.

  • Step 9 βˆ’ If none of the case holds, increment the counter to next position to check for the desired substring.

Example

The following C++ code snippet illustrates the procedure of computation of non- overlapping strings found in a given string.

// including the required libraries
#include <iostream>
using namespace std;

//function to count substrings matching the required criteria
int matchstr(string &str){

   //counter for storing the number of substrings
   int cnt = 0;

   //getting the string length
   int len = str.length();

   //iterating over the string using array indices
   for (int i = 0; i < len - 2;) {

      //matching the substrings at the specified positions
      //if string matches "010"
      if (str[i] == '0' && str[i + 1] == '1' && str[i + 2] == '0'){

         //incrementing the counter by 3 to get next substring
         i += 3;

         //increasing the found substrings counter by 1
         cnt+=1;
      }

      //if string matches "101"
      else if (str[i] == '1' && str[i + 1] == '0' && str[i + 2] == '1'){

         //incrementing the counter by 3 to get next substring
         i += 3;

         //increasing the found substrings counter by 1
         cnt+=1;
      } else {

         //check for next index
         i+=1;
      }
   }
   return cnt;
}

//calling the main function
int main(){
   string str = "110100110101";

   //returning the count of substrings matching the criteria
   int cnt = matchstr(str);
   cout<< "The entered String : "<< str;
   cout << "\nTotal strings matching :" << cnt;
   return 0;
}

Output

The entered String : 110100110101
Total strings matching :2

Explanation: There are two substrings satisfying the criteria, one is from the index position str[1:3] = 101 and str[8:10] = 010.

Conclusion

The sliding window concept is practically very useful, since it can be conveniently used for matching substrings. The length of the window is kept equivalent to the length of the substrings to be found. The time complexity of the above approach is O(n), where n is the length of the string, since a loop is required to iterate through the string characters. The space complexity of the above approach is O(n), since the string is coerced into a character array consisting of n characters of the string.

Updated on: 15-Mar-2023

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements