1332. Remove Palindromic Subsequences
Question
You are given a string
s
consisting only of letters'a'
and'b'
. In a single step you can remove one palindromic subsequence froms
.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.
Solution
当字符为空时,返回0。
注意本题中的子序列不需要连续。
由于只有’a’与’b’两个字符,因此最多两步即可将字符串清空。
辅助方法判断字符串是否为回文字符串。
如果为真则返回1,只需要删除一次。
如果为假则返回2,需要先删除所有的’a’,再删除所有的’b’。
Code
1 | class Solution { |
1332. Remove Palindromic Subsequences
https://xuanhe95.github.io/2022/06/09/1332-Remove-Palindromic-Subsequences/