- 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

# Longest Substring with at most X 0s and Y 1s of given String

A substring is the continuous sequence of the character from the given string which can be achieved by removing some character from the front and the end of the substring (possibly all or none). We are given a binary string and we have to find the length of the longest substring that contains at most X number zeros and Y number of ones where X and Y are given inputs.

## Sample Examples

### Input

string str = "101011"; int x = 1; int y = 2;

### Output

The length of the longest substring with at most X zeroes and Y ones is 3

### Explanation

Substring starting from 1st index of length 3 "010" is the longest substring with the given conditions.

### Input

string str = "101011100011"; int x = 3; int y = 2;

### Output

The length of the longest substring with at most X zeroes and Y ones is 5

## Get all Substrings

In this method, we are going to get all the substrings and then count the number of zeroes and ones present in them. If the number of ones or zeros is greater than the given limit then we are going to move to the next substring otherwise will compare the answer with the current substring size and update it.

We will use the nested for loops to get the substring of the particular length. For each length, we will start from the first 0th index and then will move until the total length of the string minus the current substring length index is reached.

### Examples

#include <bits/stdc++.h> using namespace std; // function to get all the length of the required substring int getLen(string str, int x, int y){ // getting length of the given string int len = str.size(); int ans = 0; // variable to store the answer // traversing over the string size wise for(int size = 1; size <= len; size++){ for(int start = 0; start <= len-size; start++){ int end = start + size; int countOnes = 0, countZeroes = 0; // traversing over the current substring for(int i = start; i < end; i++){ if(str[i] == '1'){ countOnes++; } else { countZeroes++; } } if(countOnes <= x && countZeroes <= y){ ans = max(ans, size); } } } return ans; // return the final answer } int main(){ string str = "101011"; int x = 1; int y = 2; // calling the function cout<<"The length of the longest substring with at most X zeroes and Y ones is: "<<getLen(str, x, y)<<endl; return 0; }

### Output

The length of the longest substring with at most X zeroes and Y ones is: 3

### Time and Space Complexity

The time complexity of the above code is O(N^3), where N is the size of the given string. We are using three nested for loops makes the time complexity in the power of 3.

The space complexity of the above code is constant or O(1), as we are not using any extra space here.

## Two Pointers Approach

In this approach, we are going to use the two-pointers approach to get the result in the linear time complexity.

We will create a function and get a slow and fast pointer, the fast pointer will move and will get that the current index is '1' or '0' and updates the respective counts.

If the count of the any of characters is greater as compared to the given maximum value then we have to move the slow pointer to the next position and will get the required answer.

In the main function, we will call the function and print the required value.

### Examples

#include <bits/stdc++.h> using namespace std; // function to get all the length of the required substring int getLen(string str, int x, int y){ // getting length of the given string int len = str.size(); int ans = 0; // variable to store the answer // pointers to traverse over the given string int slow = 0, fast = 0; int zeroes = 0, ones = 0; while(fast < len){ if(str[fast] == '1'){ ones++; } else { zeroes++; } if(ones > x || y < zeroes){ if(str[slow] == '1'){ ones--; } else { zeroes--; } slow++; } fast++; // updating answer ans = max(ans, fast - slow); } return ans; // return the final answer } int main(){ string str = "10101"; int x = 1; int y = 2; cout<<"The length of the longest substring with at most X zeroes and Y ones is: "<<getLen(str, x, y)<<endl; return 0; }

### Output

The length of the longest substring with at most X zeroes and Y ones is: 3

### Time and Space Complexity

The time complexity of the above code is linear or O(N), where N is the size of the given string.

The space complexity of the above code is constant or O(1), as we are not using any extra space here.

## Conclusion

In this tutorial, we have implemented a code to find the length of the longest substring with at most X zeroes and Y ones. A substring is the continuous sequence of the character from the given string which can be achieved by removing some characters from the front and the end of the substring. We have implemented two codes with time complexity of O(N^3) and O(N).