Longest Subsequence with equal 0s and 1s and all 0s before all 1s
Last Updated :
14 Jul, 2022
Given a binary string S, the task is to find the longest subsequence with that has equal number of 0s and 1s and all the 0s are present before all the 1s.
Examples:
Input: S = “0011001111”
Output: 8
Explanation: By removing the 3rd and 4th characters, the string becomes 00001111.
This is the longest possible subsequence following the given conditions.
Input: S = “11001”
Output: 2
Explanation: The longest possible subsequence satisfying the conditions is “01”
Input: S = “111100”
Output: 0
Explanation: There is no such subsequence that satisfies the conditions.
Approach: The problem can be solved based on the following idea:
For each index, we need to pick the maximum number of 0s from the starting and the maximum number of 1s from the rear end. This will maximize the length of the required subsequence.
Based on the above idea, we need to do the following:
- For each index, calculate the total number of 0s from the start and the total number of 1s from the end.
- If the 1s begin from ith index then the total length of the subsequence will be = 2 * min(0s from start till i, 1s from end till i).
- The maximum of this for all indices is the answer.
Follow the steps mentioned below to implement the idea:
- Build two arrays (say pre[] and post[]) to store the count of 0s from the start and the count of 1s from the end.
- Iterate from i = 0 to N-1:
- If S[i] is 0, increment the count of 0s and store it in pre[i].
- Iterate from i = N-1 to 0:
- If S[i] is 1, increment the count 1s from end and store it in post[i].
- Iterate the arrays from i = 0 to N-1:
- Calculate the length of the subsequence if this index is the breaking point between 1s and 0s.
- Maximum among these subsequences is the required length of the subsequence.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int longestGoodString(string s)
{
int n = s.size();
vector< int > pre(n, 0), post(n, 0);
if (s[0] == '0' )
pre[0] = 1;
for ( int i = 1; i < n; i++) {
if (s[i] == '0' )
pre[i] = pre[i - 1] + 1;
else
pre[i] = pre[i - 1];
}
if (s[n - 1] == '1' )
post[n - 1] = 1;
for ( int i = n - 2; i >= 0; i--) {
if (s[i] == '1' )
post[i] = 1 + post[i + 1];
else
post[i] = post[i + 1];
}
int ans = 0;
for ( int i = 0; i < n; i++) {
ans = max(ans, 2 * min(pre[i], post[i]));
}
return ans;
}
int main()
{
string S = "0011001111" ;
cout << longestGoodString(S);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int longestGoodString(String s)
{
int n = s.length();
int pre[] = new int [n];
int post[] = new int [n];
if (s.charAt( 0 ) == '0' )
pre[ 0 ] = 1 ;
for ( int i = 1 ; i < n; i++) {
if (s.charAt(i) == '0' )
pre[i] = pre[i - 1 ] + 1 ;
else
pre[i] = pre[i - 1 ];
}
if (s.charAt(n - 1 ) == '1' )
post[n - 1 ] = 1 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
if (s.charAt(i) == '1' )
post[i] = 1 + post[i + 1 ];
else
post[i] = post[i + 1 ];
}
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
ans = Math.max(ans,
2 * Math.min(pre[i], post[i]));
}
return ans;
}
public static void main(String[] args)
{
String S = "0011001111" ;
System.out.print(longestGoodString(S));
}
}
|
Python3
def longestGoodString(s):
n = len (s)
pre = []
post = []
for i in range (n):
pre.append( 0 )
post.append( 0 )
if (s[ 0 ] is '0' ):
pre[ 0 ] = 1
for i in range ( 1 ,n):
if (s[i] is '0' ):
pre[i] = pre[i - 1 ] + 1
else :
pre[i] = pre[i - 1 ]
if (s[n - 1 ] is '1' ):
post[n - 1 ] = 1
for i in range (n - 2 , - 1 , - 1 ):
if (s[i] is '1' ):
post[i] = 1 + post[i + 1 ]
else :
post[i] = post[i + 1 ]
ans = 0
for i in range (n):
ans = max (ans, 2 * min (pre[i], post[i]))
return ans
S = "0011001111"
print (longestGoodString(S))
|
C#
using System;
public class GFG
{
public static int longestGoodString(String s)
{
int n = s.Length;
int []pre = new int [n];
int []post = new int [n];
if (s[0] == '0' )
pre[0] = 1;
for ( int i = 1; i < n; i++) {
if (s[i] == '0' )
pre[i] = pre[i - 1] + 1;
else
pre[i] = pre[i - 1];
}
if (s[n - 1] == '1' )
post[n - 1] = 1;
for ( int i = n - 2; i >= 0; i--) {
if (s[i] == '1' )
post[i] = 1 + post[i + 1];
else
post[i] = post[i + 1];
}
int ans = 0;
for ( int i = 0; i < n; i++) {
ans = Math.Max(ans,
2 * Math.Min(pre[i], post[i]));
}
return ans;
}
public static void Main( string [] args)
{
string S = "0011001111" ;
Console.WriteLine(longestGoodString(S));
}
}
|
Javascript
<script>
const longestGoodString = (s) => {
let n = s.length;
let pre = new Array(n).fill(0), post = new Array(n).fill(0);
if (s[0] == '0' )
pre[0] = 1;
for (let i = 1; i < n; i++) {
if (s[i] == '0' )
pre[i] = pre[i - 1] + 1;
else
pre[i] = pre[i - 1];
}
if (s[n - 1] == '1' )
post[n - 1] = 1;
for (let i = n - 2; i >= 0; i--) {
if (s[i] == '1' )
post[i] = 1 + post[i + 1];
else
post[i] = post[i + 1];
}
let ans = 0;
for (let i = 0; i < n; i++) {
ans = Math.max(ans, 2 * Math.min(pre[i], post[i]));
}
return ans;
}
let S = "0011001111" ;
document.write(longestGoodString(S));
</script>
|
Time Complexity: O(N) where N is the length of the String
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...