# 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

412 Views 