Minimum number of palindromic subsequences to be removed to empty a binary string Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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. If given string is palindrome, we need only one removal. 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. Implementation: 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 subsequences to // be removed to make string empty int minRemovals(const char *str) { // If string is empty if (str[0] == '\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; } 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 // subsequences 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. 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 subsequences 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. 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 // subsequences 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 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 ?> JavaScript <script> // Javascript 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 let l = 0; let 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 // 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 document.write(minRemovals("010010") + "</br>"); document.write(minRemovals("0100101")); </script> Output1 2 Time Complexity: O(n) Auxiliary Space : O(1) 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. Comment More infoAdvertise with us Next Article Minimum number of palindromic subsequences to be removed to empty a binary string kartik Follow Improve Article Tags : Strings DSA palindrome subsequence binary-string +1 More Practice Tags : palindromeStrings Similar Reads Minimum size substring to be removed to make a given string palindromic Given a string S, the task is to print the string after removing the minimum size substring such that S is a palindrome or not. Examples: Input: S = "pqrstsuvwrqp"Output: pqrstsrqpExplanation:Removal of the substring "uvw" modifies S to a palindromic string. Input: S = "geeksforskeeg"Output: geeksfs 15+ min read Minimum number of characters to be replaced to make a given string Palindrome Given string str, the task is to find the minimum number of characters to be replaced to make a given string palindrome. Replacing a character means replacing it with any single character in the same position. We are not allowed to remove or add any characters. If there are multiple answers, print t 5 min read Minimize deletions in a Binary String to remove all subsequences of the form "0101" 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. Examples: Input: S = "0101101"Output: 2Explanation: Removing S[1] and S[5] modifies the string to 00111. 6 min read Maximum count of â010..â subsequences that can be removed from given Binary String Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S. Examples: Input: S = "110011010"Output: 3Explanation:Following are the subsequence removed:Operation 1: Ch 6 min read Minimum number of characters to be removed to make a binary string alternate Given a binary string, the task is to find minimum number of characters to be removed from it so that it becomes alternate. A binary string is alternate if there are no two consecutive 0s or 1s.Examples : Input : s = "000111" Output : 4 We need to delete two 0s and two 1s to make string alternate. I 4 min read Minimum number of deletions to make a string palindrome | Set 2 Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome. Examples: Input : baca Output : 1 Input : geek Output : 2 We have discussed one approach in below post. Minimum number of deletions to make a string palindrome Below approach will use 9 min read Form minimum number of Palindromic Strings from a given string Given a string S, the task is to divide the characters of S to form minimum number of palindromic strings. Note: There can be multiple correct answers. Examples: Input: S = "geeksforgeeks"Output: {eegksrskgee, o, f} Explanation: There should be at least 3 strings "eegksrskgee", "o", "f". All 3 forme 12 min read Find a palindromic string B such that given String A is a subsequence of B Given a string A . Find a string B , where B is a palindrome and A is a subsequence of B. A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subs 6 min read Count of Subsequences forming Palindrome with Binary representations Given an array A[]. The task is to count the number of subsequences such that concatenation of binary representation of the numbers represents palindrome. Examples: Input: A[] = {1, 2, 3} Output: 4Explanation: Binary representation of all elements are {1, 10, 11} here the concatenation of {1, 3} = 10 min read Minimum moves to make String Palindrome incrementing all characters of Substrings Given a string S of length N, the task is to find the minimum number of moves required to make a string palindrome where in each move you can select any substring and increment all the characters of the substring by 1. Examples: Input: S = "264341"Output: 2?Explanation: We can perform the following 6 min read Like