Given a binary string S of length N, the task is to find the minimum number of characters required to be deleted from the string such that no subsequence of the form “0101” exists in the string.
Input: S = “0101101”
Explanation: Removing S and S modifies the string to 00111. Therefore, no subsequence of the type 0101 can be obtained from the given string.
Input: S = “0110100110”
Approach: Follow the steps below to solve the problem:
- The required valid string can consist of at most three blocks of the same elements i.e. the strings can be one of the following patterns “00…0”, “11…1”, “00…01…1”, “1…10..0”, “00..01…10..0”, “1…10…01…1”.
- Count frequencies of 0s and 1s of a block using partial sum.
- Fix the starting and ending indices of blocks of 0s and 1s and determine the minimum number of characters required to be deleted by the calculated partial sums.
- Therefore, check the length of the longest string that can be obtained by removing the subsequences of the given type.
Below is the implementation of the above approach:
Time Complexity: O(N2)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions so that no two consecutive are same
- Minimum deletions required such that any number X will occur exactly X times
- Minimize sum of smallest elements from K subsequences of length L
- Minimize cost to convert all characters of a binary string to 0s
- Number of subsequences of the form a^i b^j c^k
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Minimize length of prefix of string S containing all characters of another string T
- Number of subsequences in a given binary string divisible by 2
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Form N-copy string with add, remove and append operations
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string using ArrayList
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsaini