[
palindrome
string
]
Leetcode 1332. Remove Palindromic Subsequences
https://leetcode.com/problems/remove-palindromic-subsequences
Please note, that we deal with subsequence, not subarrays, so we can delete any amount of symbols on each step, not only adjacent ones!
There can be basically 3
different answers for this problem:
- We return
0
, if string is already empty, we do not need to do anything. - If string is palindrome itself and it is not empty, we return
1
. - Finally, if two previous cases are not true, we can first delete all letters
a
and then all lettersb
, so we return2
.
Complexity: time complexity is O(n)
, we need to check if the string is palindrome. Space complexity is O(n)
, because here I create s[::-1]
string, which can be reduced to O(1)
space.
class Solution:
def removePalindromeSub(self, s):
return 0 if s == "" else 1 if s==s[::-1] else 2
If you like the solution, you can upvote it on leetcode discussion section: Problem 1332