Minimum number of palindromic subsequences to be removed to empty a binary string Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : Strings DSA palindrome subsequence binary-string +1 More Practice Tags : palindromeStrings Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like