Minimum number of palindromic subsequences to be removed to empty a binary string
Given a binary string, count minimum number of subsequences to be removed to make it an empty string.
Examples :
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)
We strongly recommend that you click here and practice it, before moving on to the solution.
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.
C++
// C++ program to count minimum palindromic subsequences // to be removed to make an string empty. #include <bits/stdc++.h> using namespace std; // A function to check if a string str is palindrome bool isPalindrome(const char *str) { // Start from leftmost and rightmost corners of str int l = 0; int h = strlen(str) - 1; // Keep comparing characters while they are same while (h > l) if (str[l++] != str[h--]) return false; return true; } // Returns count of minimum palindromic subseuqnces to // be removed to make string empty int minRemovals(const char *str) { // If string is empty if (str[0] == '') return 0; // If string is palindrome if (isPalindrome(str)) return 1; // If string is not palindrome return 2; } // Driver code to test above int main() { cout << minRemovals("010010") << endl; cout << minRemovals("0100101") << endl; return 0; } |
chevron_right
filter_none
Java
// Java program to count minimum palindromic // subsequences to be removed to make // an string empty. import java.io.*; class GFG { // A function to check if a string // str is palindrome static boolean isPalindrome(String str) { // Start from leftmost and rightmost // corners of str int l = 0; int h = str.length() - 1; // Keep comparing characters // while they are same while (h > l) if (str.charAt(l++) != str.charAt(h--)) return false; return true; } // Returns count of minimum palindromic // subseuqnces to be removed to // make string empty static int minRemovals(String str) { // If string is empty if (str.charAt(0) == '') return 0; // If string is palindrome if (isPalindrome(str)) return 1; // If string is not palindrome return 2; } // Driver code to test above public static void main (String[] args) { System.out.println (minRemovals("010010")); System.out.println (minRemovals("0100101")); } } // This code is contributed by vt_m. |
chevron_right
filter_none
Python3
# Python program to count minimum # palindromic subsequences to # be removed to make an string # empty. # A function to check if a # string str is palindrome def isPalindrome(str): # Start from leftmost and # rightmost corners of str l = 0 h = len(str) - 1 # Keep comparing characters # while they are same while (h > l): if (str[l] != str[h]): return 0 l = l + 1 h = h - 1 return 1 # Returns count of minimum # palindromic subseuqnces to # be removed to make string # empty def minRemovals(str): #If string is empty if (str[0] == ''): return 0 #If string is palindrome if (isPalindrome(str)): return 1 # If string is not palindrome return 2 # Driver code print(minRemovals("010010")) print(minRemovals("0100101")) # This code is contributed by Sam007. |
chevron_right
filter_none
C#
// C# program to count minimum palindromic // subsequences to be removed to make // an string empty. using System; class GFG { // A function to check if a // string str is palindrome static bool isPalindrome(String str) { // Start from leftmost and // rightmost corners of str int l = 0; int h = str.Length - 1; // Keep comparing characters // while they are same while (h > l) if (str[l++] != str[h--]) return false; return true; } // Returns count of minimum palindromic // subseuqnces to be removed to // make string empty static int minRemovals(String str) { // If string is empty if (str[0] == '') return 0; // If string is palindrome if (isPalindrome(str)) return 1; // If string is not palindrome return 2; } // Driver code to public static void Main () { Console.WriteLine(minRemovals("010010")); Console.WriteLine(minRemovals("0100101")); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php // PHP program to count minimum // palindromic subsequences to // be removed to make an string empty. // A function to check if a // string str is palindrome function isPalindrome($str) { // Start from leftmost and // rightmost corners of str $l = 0; $h = strlen($str) - 1; // Keep comparing characters // while they are same while ($h > $l) if ($str[$l++] != $str[$h--]) return false; return true; } // Returns count of minimum // palindromic subsequences // to be removed to make // string empty function minRemovals($str) { // If string is empty if ($str[0] == '') return 0; // If string is palindrome if (isPalindrome($str)) return 1; // If string is not palindrome return 2; } // Driver Code echo minRemovals("010010"), "\n"; echo minRemovals("0100101") , "\n"; // This code is contributed by ajit ?> |
chevron_right
filter_none
Output :
1 2
Exercises:
- 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
Recommended Posts:
- Minimum number of characters to be removed to make a binary string alternate
- Minimum cuts required to convert a palindromic string to a different palindromic string
- 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 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
- 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
- Make palindromic string non-palindromic by rearranging its letters


