Back to photostream

Remove Palindromic Subsequences Leetcode Solution

The problem Remove Palindromic Subsequences Leetcode Solution states that you are given a string. The string consists of only two characters 'a' or 'b'. You are required to erase the whole string. There is a restriction that you can delete only a palindromic subsequence in one move. Find the minimum number of steps required to erase the whole string. Let us take a look at a few examples before jumping into the solution.

 

s = "ababa"

1

Explanation: Since the string is a palindrome. We can remove the whole string in a single move. Thus the answer is also 1.

s = "abb"

2

Explanation: In the first move, we remove "bb". In the second move, we remove "a". Thus we require at least 2 moves to erase the whole string.

Approach for Remove Palindromic Subsequences Leetcode Solution

The problem Remove Palindromic Subsequences Leetcode Solution is an observation one. It requires us to observe that the string consists of only two characters 'a' and 'b'. If we come across a palindrome, we simply return 1. Because it requires a single move to erase a whole palindrome. If we get an empty string, we should return 0. But other than these, there is only a single case, when we have a string that is not palindrome as a whole.

 

But since the string has only 'a' and 'b'. We will take at most 2 moves to remove all the characters. In the first move, we should remove all 'a's. In the second move, we remove all 'b's. Thus the answer to this p !=

 

www.tutorialcup.com/leetcode-solutions/remove-palindromic...

37 views
0 faves
0 comments
Uploaded on September 1, 2021