[LeetCode] Remove Palindromic Subsequences

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

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
int isPalindrome(string& s) {
int start = 0, mid = s.length() / 2, end = s.length() - 1;
for(; start <= mid; start++, end--)
if(s[start] != s[end])
return 2;
return 1;
}
public:
int removePalindromeSub(string s) {
return s.length() == 0 ? 0 : isPalindrome(s);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/03/08/PS/LeetCode/remove-palindromic-subsequences/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.