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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 cuts required to convert a palindromic string to a different palindromic string
- Minimum number of subsequences required to convert one string to another
- 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 replacements to make the binary string alternating | Set 2
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of swaps to make two binary string equal
- 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 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9