Given a binary string, find the longest subsequence of the form (0)*(1)*(0)* in it. Basically we need to divide the string into 3 non overlapping strings (these strings might be empty) without changing the order of letters. First and third strings are made up of only 0 and the second string is made up of only 1. These strings could be made by deleting some characters in original string. What is the maximum size of string, we can get?
Input : 000011100000 Output : 12 Explanation : First part from 1 to 4. Second part 5 to 7. Third part from 8 to 12 Input : 100001100 Output : 8 Explanation : Delete the first letter. First part from 2 to 4. Second part from 5 to 6. Last part from 7. Input : 00000 Output : 5 Explanation : Special Case of Only 0 Input : 111111 Output : 6 Explanation : Special Case of Only 1 Input : 0000001111011011110000 Output : 20 Explanation : Second part is from 7 to 18. Remove all the 0 between indices 7 to 18.
A simple solution is to generate all subsequences of given sequence. For every subsequence, check if it is in given form. If yes, compare it with result so far and update the result if needed.
This problem can be efficiently solved by pre-computing below arrays in O(n^2) time.
pre_count_0[i] be the count of letter 0 in the prefix of string till index i.
pre_count_1[i] be the count of letter 1 in the prefix of string till length i.
post_count_0[i] be the count of letter 0 in the suffix string from index i till index n (here n is the size of string).
Now we fix two two positions i and j, 1 <=i <= j <=n. We will remove all 0 from substring which starts at index i and ends in index j. Thus this makes the second substring of only 1. In the prefix before the index i and in the postfix after the index j we will delete all 1 and thus it will make first and third part of the string.
then the maximum length of string attainable is max of
pre_count_0[i-1] + (pre_count_1[j]-pre_count_1[i-1]) + pre_count_1[j+1]
Special cases : When String is made of only 0s or 1s, ans is n where n is length of string.
This article is contributed by nikhil ranjan 7. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find length of longest subsequence of one string which is substring of another string
- Longest subsequence with at least one character appearing in every string
- Longest sub string of 0's in a binary string which is repeated K times
- Length of longest consecutive ones by at most one swap in a Binary String
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum length subsequence possible of the form R^N K^N
- Longest subsequence with a given AND value | O(N)
- Longest subsequence with no 0 after 1
- Longest subsequence with a given AND value
- Longest Zig-Zag Subsequence
- Longest subsequence whose average is less than K
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence using BIT
- Longest Uncommon Subsequence
- Longest Repeating Subsequence