Given a binary string, count minimum number of subsequences to be removed to make it an empty string.
Input: str = "10001" Output: 1 Since the whole string is palindrome, we need only one removal. Input: str = "10001001" Output: 2 We can remove the middle 1 as first removal, after first removal string becomes 1000001 which is a palindrome.
Expected time complexity : O(n)
The problem is simple and can be solved easily using below two facts.
1) If given string is palindrome, we need only one removal.
2) Else we need two removals. Note that every binary string has all 1’s as a subsequence and all 0’s as another subsequence. We can remove any of the two subsequences to get a unary string. A unary string is always palindrome.
- Extend the above solution to count minimum number of subsequences to be removed to make it an empty string.
- What is the maximum count for ternary strings
This problem and solution are contributed by Hardik Gulati. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Minimum number of characters to be removed to make a binary string alternate
- Number of subsequences in a given binary string divisible by 2
- Minimum number of subsequences required to convert one string to another
- Minimum palindromic subarray removals to make array Empty
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Number of palindromic subsequences of length k where k <= 3
- Minimum number of operations required to sum to binary string S
- Find the minimum number of elements that should be removed to make an array good
- Minimum number of swaps to make two binary string equal
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of replacements to make the binary string alternating | Set 2
- Number of K length subsequences with minimum sum
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Number of subsequences in a string divisible by n
- Number of subsequences as "ab" in a string repeated K times
- Number of ways to partition a string into two balanced subsequences