- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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, b
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.