Given a binary string s, the task is to find the length of the longest subsequence that can be divided into three substrings such that the first and third substrings are either empty or filled with 1 and the substring at the middle is either empty or filled with 0.
Examples:
Input: s = “1001”
Output: 4
Explanation:
The entire string can be divided into the desired three parts: “1”, “00”, “1”.
Input: s = “010”
Output: 2
Explanation:
The subsequences “00”, “01” and “10” can be split into three desired parts {“”, “00”, “”}, {“”, “0”, “1”} and {“1”, “0”, “”}
Approach:
To solve the problem, we need to follow the steps given below:
- Firstly, pre-compute and store in prefix arrays, the occurrences of ‘1’ and ‘0’ respectively.
- Initialize two integers i and j, where i will be the point of partition between the first and second string and j will be the point of partition between the second and third strings.
- Iterate over all possible values of i & j (0 <= i < j <=n) and find the maximum possible length of the subsequence possible which satisfies the given condition.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubseq(string s, int length)
{
int ones[length + 1], zeroes[length + 1];
memset (ones, 0, sizeof (ones));
memset (zeroes, 0, sizeof (zeroes));
for ( int i = 0; i < length; i++) {
if (s[i] == '1' ) {
ones[i + 1] = ones[i] + 1;
zeroes[i + 1] = zeroes[i];
}
else {
zeroes[i + 1] = zeroes[i] + 1;
ones[i + 1] = ones[i];
}
}
int answer = INT_MIN;
int x = 0;
for ( int i = 0; i <= length; i++) {
for ( int j = i; j <= length; j++) {
x += ones[i];
x += (zeroes[j] - zeroes[i]);
x += (ones[length] - ones[j]);
answer = max(answer, x);
x = 0;
}
}
cout << answer << endl;
}
int main()
{
string s = "10010010111100101" ;
int length = s.length();
longestSubseq(s, length);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void longestSubseq(String s,
int length)
{
int [] ones = new int [length + 1 ];
int [] zeroes = new int [length + 1 ];
for ( int i = 0 ; i < length; i++)
{
if (s.charAt(i) == '1' )
{
ones[i + 1 ] = ones[i] + 1 ;
zeroes[i + 1 ] = zeroes[i];
}
else
{
zeroes[i + 1 ] = zeroes[i] + 1 ;
ones[i + 1 ] = ones[i];
}
}
int answer = Integer.MIN_VALUE;
int x = 0 ;
for ( int i = 0 ; i <= length; i++)
{
for ( int j = i; j <= length; j++)
{
x += ones[i];
x += (zeroes[j] - zeroes[i]);
x += (ones[length] - ones[j]);
answer = Math.max(answer, x);
x = 0 ;
}
}
System.out.println(answer);
}
public static void main(String[] args)
{
String s = "10010010111100101" ;
int length = s.length();
longestSubseq(s, length);
}
}
|
Python3
import sys
def longestSubseq(s, length):
ones = [ 0 for i in range (length + 1 )]
zeroes = [ 0 for i in range (length + 1 )]
for i in range (length):
if (s[i] = = '1' ):
ones[i + 1 ] = ones[i] + 1
zeroes[i + 1 ] = zeroes[i]
else :
zeroes[i + 1 ] = zeroes[i] + 1
ones[i + 1 ] = ones[i]
answer = - sys.maxsize - 1
x = 0
for i in range (length + 1 ):
for j in range (i, length + 1 ):
x + = ones[i]
x + = (zeroes[j] - zeroes[i])
x + = (ones[length] - ones[j])
answer = max (answer, x)
x = 0
print (answer)
S = "10010010111100101"
length = len (S)
longestSubseq(S, length)
|
C#
using System;
class GFG{
static void longestSubseq(String s,
int length)
{
int [] ones = new int [length + 1];
int [] zeroes = new int [length + 1];
for ( int i = 0; i < length; i++)
{
if (s[i] == '1' )
{
ones[i + 1] = ones[i] + 1;
zeroes[i + 1] = zeroes[i];
}
else
{
zeroes[i + 1] = zeroes[i] + 1;
ones[i + 1] = ones[i];
}
}
int answer = int .MinValue;
int x = 0;
for ( int i = 0; i <= length; i++)
{
for ( int j = i; j <= length; j++)
{
x += ones[i];
x += (zeroes[j] - zeroes[i]);
x += (ones[length] - ones[j]);
answer = Math.Max(answer, x);
x = 0;
}
}
Console.WriteLine(answer);
}
public static void Main(String[] args)
{
String s = "10010010111100101" ;
int length = s.Length;
longestSubseq(s, length);
}
}
|
Javascript
<script>
function longestSubseq(s, len) {
var ones = new Array(len + 1).fill(0);
var zeroes = new Array(len + 1).fill(0);
for ( var i = 0; i < len; i++) {
if (s[i] === "1" ) {
ones[i + 1] = ones[i] + 1;
zeroes[i + 1] = zeroes[i];
}
else {
zeroes[i + 1] = zeroes[i] + 1;
ones[i + 1] = ones[i];
}
}
var answer = -2147483648;
var x = 0;
for ( var i = 0; i <= len; i++) {
for ( var j = i; j <= len; j++) {
x += ones[i];
x += zeroes[j] - zeroes[i];
x += ones[len] - ones[j];
answer = Math.max(answer, x);
x = 0;
}
}
document.write(answer);
}
var s = "10010010111100101" ;
var len = s.length;
longestSubseq(s, len);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)