VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-palindromic-subsequences-to-be-removed-to-empty-a-binary-string/

⇱ Minimum palindromic subsequences to be removed to empty a binary string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum palindromic subsequences to be removed to empty a binary string

Last Updated : 31 May, 2026

Given a Binary String s (the string that contains only 0's and 1's). Find the minimum number of palindromic subsequences that are needed to be removed such that string s becomes empty.

Examples :

Input: s = "10001"
Output: 1
Explanation: We can remove whole s, as s is already a palindrome.

Input: N = 8, S = "10001001"
Output: 2
Explanation: First we'll remove sub-sequence "111" and then "00000".

Using Palindrome Property - O(n) Time and O(1) Space

The idea is based on the observation that if the entire binary string is already a palindrome, it can be removed in a single operation. If it is not a palindrome, the answer is always 2 because all occurrences of one character (0 or 1) can be removed in one operation and all occurrences of the other character can be removed in another operation.

  • If the string is empty, return 0
  • Check whether the entire string is a palindrome: If yes, return 1
  • Otherwise, return 2

Output
1


Comment