Skip to main content

Remove Palindromic Subsequences - Solution & Explanation

EasyTwo PointersString12 min readAsked at: Amazon
Practice this problem

Problem Statement

You are given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.

Return the minimum number of steps to make the given string empty.

A string is a subsequence of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does not necessarily need to be contiguous.

A string is called palindrome if is one that reads the same backward as well as forward.

 

Example 1:

Input: s = "ababa"
Output: 1
Explanation: s is already a palindrome, so its entirety can be removed in a single step.

Example 2:

Input: s = "abb"
Output: 2
Explanation: "abb" -> "bb" -> "". 
Remove palindromic subsequence "a" then "bb".

Example 3:

Input: s = "baabb"
Output: 2
Explanation: "baabb" -> "b" -> "". 
Remove palindromic subsequence "baab" then "b".

 

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either 'a' or 'b'.

Approach Overview

Problem Overview: You are given a string s containing only the characters 'a' and 'b'. In one operation, you can remove any subsequence that forms a palindrome. The task is to return the minimum number of operations required to delete the entire string.

Approach 1: Check If String is a Palindrome (O(n) time, O(1) space)

The key observation: because the string contains only two characters (a and b), the answer can never exceed 2. If the entire string is already a palindrome, you can remove it in one operation. Otherwise, remove all a characters in one step and all b characters in another. To verify the first case, use the two pointers technique—place one pointer at the start and one at the end, then move inward while comparing characters. If every pair matches, the string is a palindrome and the answer is 1. If any mismatch appears, the answer must be 2.

This works because any sequence of identical characters is itself a palindrome. Even if the full string is not symmetric, removing all as forms one palindromic subsequence and removing all bs forms another. The check itself is linear since each character is visited once.

Approach 2: Count Unique Characters and Remove (O(n) time, O(1) space)

Another way to reason about the problem is by counting distinct characters in the string. Since subsequences do not need to be contiguous, all occurrences of the same character can be removed together as a palindrome (for example, "aaaa"). Scan the string and track the set of unique characters using simple frequency checks. If only one character appears, the whole string is already a palindrome and can be removed in one operation.

If two characters exist (a and b), you can always remove them in exactly two steps: remove the subsequence containing all as, then remove all bs. This approach treats the problem as a property of the string composition rather than checking symmetry. The scan is linear and uses constant extra memory.

Recommended for interviews: The palindrome check using two pointers is usually expected. It shows you recognize the constraint that the answer is at most two and can verify the palindrome condition efficiently. The character-count reasoning is equally valid but the two-pointer explanation demonstrates stronger familiarity with common two pointer patterns used in string problems.

Approach 1: Check If String is a Palindrome

If the input string is already a palindrome, then we can remove it in one step. If not, since the string contains only 'a' and 'b', we can always remove all 'a's in one step and all 'b's in another step, making at most two steps. Hence, the minimum number of steps to make the string empty is either 1 (if the string is a palindrome) or 2.

The function isPalindrome checks if the string is the same forwards and backwards. If it is a palindrome, the string can be removed in one step. Otherwise, since the string is composed of only 'a's and 'b's, we return 2 (removing all 'a's in one step and all 'b's in another step).

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of string s, as we need to potentially traverse the whole string to check if it's a palindrome.
Space Complexity: O(1), as we use only a few extra variables.

Try this approach in the editor →

Approach 2: Count Unique Characters and Remove

The second approach is based on the observation that the given string only consists of 'a' and 'b'. We can count the unique characters in the string. If the string is non-empty, it will always contain either 'a's or 'b's or both. If it contains both, removing all of one type and then the other would take two steps.

This C function checks if both 'a' and 'b' exist in the string using strchr. If both are found, it returns 2. Otherwise, if only one type exists, it only takes 1 step to remove.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Check If String is a Palindrome

Time Complexity: O(n), where n is the length of string s, as we need to potentially traverse the whole string to check if it's a palindrome.
Space Complexity: O(1), as we use only a few extra variables.

Count Unique Characters and Remove

Time Complexity: O(n).
Space Complexity: O(1).

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Check if Entire String is Palindrome (Two Pointers)O(n)O(1)Best general solution. Quickly determines if the whole string can be removed in one step.
Count Unique CharactersO(n)O(1)Useful when reasoning about character composition rather than symmetry.

Video Solution

Leetcode 1332. Remove Palindromic SubsequencesFraz12,252 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Remove Palindromic Subsequences easy or hard?
Remove Palindromic Subsequences is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge lies in recognizing the key observation that only two operations are ever needed due to the limited character set.
Remove Palindromic Subsequences Python/Java solution
Most implementations check whether the string equals its reverse (Python) or use two pointers moving inward (Java/C++). If the string is empty return 0, if it is a palindrome return 1, otherwise return 2. The logic is simple and runs in linear time.
How to solve Remove Palindromic Subsequences in O(n)?
Traverse the string using two pointers from both ends. If all mirrored characters match, the string is a palindrome and the answer is 1. If a mismatch appears, the answer becomes 2 because you can remove all 'a' characters in one operation and all 'b' characters in another. The scan touches each character once, giving O(n) time complexity.
What is the best approach for Remove Palindromic Subsequences?
The optimal approach checks whether the entire string is already a palindrome using the two pointers technique. If the string is a palindrome, it can be removed in one operation. Otherwise, remove all 'a' characters in one step and all 'b' characters in another, resulting in at most two operations. This solution runs in O(n) time and O(1) space.
Is Remove Palindromic Subsequences asked at Google/Amazon/Meta?
Palindrome and string manipulation problems appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact problem may not always appear, its core ideas—two pointers and palindrome checks—are common interview patterns.
What data structure is used in Remove Palindromic Subsequences?
The main technique relies on two pointers for palindrome verification. Some explanations also use a small set or frequency counter to track unique characters in the string. Both approaches operate with constant extra memory.
What is the time complexity of Remove Palindromic Subsequences?
The optimal solution runs in O(n) time where n is the length of the string. You perform a single pass to check whether the string is a palindrome or to inspect the characters present. Space complexity remains O(1) because only a few pointers or counters are required.

Ready to solve this problem?

Practice Remove Palindromic Subsequences with our built-in code editor and test cases.

Practice on FleetCode