Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle

Given a binary string s, the task is to find the length of 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 subsequence “00”, “01” and “10” can be split into desired three 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 occurences of ‘1’ and ‘0’ respectively.
  • Initialize two integers i and j, where i will be the point of partition between first and second string and j will be the point of partition between second and third string.
  • 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the
// longest subsequence possible
// that starts and ends with 1
// and filled with 0 in the middle
  
#include <bits/stdc++.h>
using namespace std;
  
int longestSubseq(string s, int length)
{
    // Prefix array to store the
    // occurences of '1' and '0'
    int ones[length + 1], zeroes[length + 1];
  
    // Initialise prefix arrays with 0
    memset(ones, 0, sizeof(ones));
    memset(zeroes, 0, sizeof(zeroes));
  
    // Iterate over the length of the string
    for (int i = 0; i < length; i++) {
  
        // If current character is '1'
        if (s[i] == '1') {
            ones[i + 1] = ones[i] + 1;
            zeroes[i + 1] = zeroes[i];
        }
  
        // If current character is '0'
        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++) {
            // Add '1' available for
            // the first string
            x += ones[i];
  
            // Add '0' available for
            // the second string
            x += (zeroes[j] - zeroes[i]);
  
            // Add '1' available for
            // the third string
            x += (ones[length] - ones[j]);
  
            // Update answer
            answer = max(answer, x);
  
            x = 0;
        }
    }
  
    // Print the final result
    cout << answer << endl;
}
  
// Driver Code
int main()
{
  
    string s = "10010010111100101";
  
    int length = s.length();
  
    longestSubseq(s, length);
  
    return 0;
}

chevron_right


Output:

12

Time Complexity: O(N2)
Auxillary Space: O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.