Longest Substring with at most X 0s and Y 1s of given String
Last Updated :
11 Apr, 2022
Given a binary string S of length N, the task is to find the longest substring with at most X number of 0’s and Y number of 1’s.
Example:
Input: S = “10101”, N = 5, X = 1, Y = 2
Output: 3
Explanation: The longest substring with at most 1 zero and 2 ones is “101” with length 3.
Input: S = “111”, N = 3, X = 1, Y = 1
Output: 1
Explanation: longest substring with at most 1 zero and 1 one is “1” with length 1.
Input: S = “00000”, N = 5, X = 0, Y = 1
Output: 0
Explanation: No substrings exists with at most 0 number of zero’s and 1 number of one’s as whole string does not contain 1’s anywhere.
Naive Approach: The naive approach to solve this problem is to find all the substrings and for each substring count the number of 0’s and 1’s in the substring and check if the counts of those are at most X and Y respectively.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: This problem can be solved using the concept of two pointer approach based on the following idea:
Use two pointers (i, j), Where j is the last pointer and i is the starting pointer. Use those pointers to keep track of number of 0s and 1s in that range and update those based on the count of 0s and 1s in that range to adjust the substring length.
- Increment the last pointer till the any one of the count of 0s and 1s does not exceed their upper limit.
- If any of their count exceeds the provided limit then increment the first pointer to decrease the range.
- The longest range will give the longest substring.
Follow the below steps to solve this problem.
- Initialize i, j and maxLength to 0.
- Traverse while j < N.
- Count the number of 0’s and 1’s which is ending at jth index.
- Check if the number of 0’s and 1’s satisfies the given condition.
- If yes, then calculate the size of the current substring which is starting at i and ending at j.
- If the calculated size is greater than maxLength then, update the maxLength.
- Otherwise, reduce the count of characters which is at ith index and increment the value of i.
- Return maxLength as the final answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int longestKSubarray(string& S, int X, int Y)
{
int N = S.length(), i = 0, j = 0;
int count0 = 0, count1 = 0, maxLength = 0;
while (j < N) {
if (S[j] == '0' ) {
count0++;
}
else {
count1++;
}
if (count0 > X || count1 > Y) {
if (S[i] == '0' ) {
count0--;
}
else {
count1--;
}
i++;
}
j++;
maxLength = max(maxLength, j - i);
}
return maxLength;
}
int main()
{
string S = "10101" ;
int X = 1, Y = 2;
cout << longestKSubarray(S, X, Y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int longestKSubarray(String S, int X, int Y)
{
int N = S.length(), i = 0 , j = 0 ;
int count0 = 0 , count1 = 0 , maxLength = 0 ;
while (j < N) {
if (S.charAt(j) == '0' ) {
count0++;
}
else {
count1++;
}
if (count0 > X || count1 > Y) {
if (S.charAt(i) == '0' ) {
count0--;
}
else {
count1--;
}
i++;
}
j++;
maxLength = Math.max(maxLength, j - i);
}
return maxLength;
}
public static void main (String[] args) {
String S = "10101" ;
int X = 1 , Y = 2 ;
System.out.print(longestKSubarray(S, X, Y));
}
}
|
Python3
def longestKSubarray(S, X, Y):
N = len (S)
i = 0
j = 0
count0 = 0
count1 = 0
maxLength = 0
while j < N:
if S[j] = = "0" :
count0 + = 1
else :
count1 + = 1
if count0 > X or count1 > Y:
if S[i] = = "0" :
count0 - = 1
else :
count1 - = 1
i + = 1
j + = 1
maxLength = max (maxLength, j - i)
return maxLength
S = "10101"
X = 1
Y = 2
print (longestKSubarray(S, X, Y))
|
C#
using System;
class GFG {
static int longestKSubarray( string S, int X, int Y)
{
int N = S.Length, i = 0, j = 0;
int count0 = 0, count1 = 0, maxLength = 0;
while (j < N) {
if (S[j] == '0' ) {
count0++;
}
else {
count1++;
}
if (count0 > X || count1 > Y) {
if (S[i] == '0' ) {
count0--;
}
else {
count1--;
}
i++;
}
j++;
maxLength = Math.Max(maxLength, j - i);
}
return maxLength;
}
public static void Main()
{
string S = "10101" ;
int X = 1, Y = 2;
Console.Write(longestKSubarray(S, X, Y));
}
}
|
Javascript
<script>
const longestKSubarray = (S, X, Y) => {
let N = S.length, i = 0, j = 0;
let count0 = 0, count1 = 0, maxLength = 0;
while (j < N) {
if (S[j] == '0' ) {
count0++;
}
else {
count1++;
}
if (count0 > X || count1 > Y) {
if (S[i] == '0' ) {
count0--;
}
else {
count1--;
}
i++;
}
j++;
maxLength = Math.max(maxLength, j - i);
}
return maxLength;
}
let S = "10101" ;
let X = 1, Y = 2;
document.write(longestKSubarray(S, X, Y));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...