Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Remove Palindromic Subsequences

XPChallenge Points: 10
levelLevel: Easy

You are given a string s that contains only the characters 'a' and 'b'. In one move, you are allowed to delete any subsequence of the string as long as that subsequence forms a palindrome.

Your goal is to remove all characters from the string using the minimum number of such operations.

A subsequence is a sequence that can be formed by deleting some characters without changing the order of the remaining ones.
A palindrome reads the same forward and backward.

Return the minimum number of operations needed to completely delete the string.

Example 1:

Input: s = "aaaa"

Output: 1

Explanation:

The entire string is a palindrome, so removing it once is enough.

Example 2:

Input: s = "baba"

Output: 2

Explanation:

One possible sequence: Step 1: remove "bbb" (palindrome subsequence → actually "bab"). Step 2: remove the remaining "a".

Example 3:

Input: s = "ababbab"

Output: 2

Explanation:

Step 1: delete "ababba" (palindromic subsequence). Step 2: delete the last "b".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result