You're given a string s consisting only of the letters 'a' and 'b'. Your task is to completely empty this string by removing palindromic subsequences in the minimum number of steps possible.
In each step, you can remove one palindromic subsequence from the string. A subsequence doesn't need to be contiguous - you can pick characters from anywhere in the string as long as you maintain their relative order.
Key insight: Since we only have two characters ('a' and 'b'), this problem has a surprisingly elegant solution! Think about what palindromic subsequences you can always form with just these two letters.
Goal: Return the minimum number of steps needed to make the string empty.
Input & Output
Visualization
Time & Space Complexity
Generate 2^n subsequences, check each for palindrome in O(n) time
Store all possible subsequences
Constraints
- 1 โค s.length โค 1000
- s[i] is either 'a' or 'b'
- Key insight: With only 2 distinct characters, answer is always 0, 1, or 2