1332. Remove Palindromic Subsequences

https://leetcode.com/problems/remove-palindromic-subsequences/

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.

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: String is already palindrome

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".

Example 4:

只由a和b组成的字符串s每步能移除掉一个回文序列,问把整个字符串清空至少需要多少步。分为三种情况:1. 空字符串,返回0;2. s本身已是回文,返回1;3. 其它,第一步清掉所有的a,第二步清掉b,最多需要两步。

Last updated

Was this helpful?