Today, we will solve the 8th problem of the March LeetCoding Challenge.
Problem Statement
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:** "**a**bb" -> "**bb**" -> "".
Remove palindromic subsequence "a" then "bb".
Example 3:
**Input:** s = "baabb"
**Output:** 2
**Explanation:** "**baa**b**b**" -> "b" -> "".
Remove palindromic subsequence "baab" then "b".
Example 4:
**Input:** s = ""
**Output:** 0
Solution
If we get an empty string, we can return 0 because there is nothing to delete.
If we have a palindrome, we can return 1 because a palindrome can be removed at once.
If not, we can return 2. (At first we can remove ‘a’ or ‘b’ and then we can remove the remaining string)
Time complexity:O(n), n is the length of the string
Space Complexity:O(1), no additional space is used
The code can be found in the following repository.
LeetCode
I have been solving Leetcode problems for around a year or so. Suddenly I have developed a passion of writing tutorials for these problems. I am starting with Leetcode problem, in future I will try to make tutorials on Spring,Android,Java,Algorithms and many more.
Follow me on medium - https://sourav-saikia.medium.com
Follow me on dev.to - https://dev.to/sksaikia
Follow me on twitter - https://twitter.com/sourav__saikia
Connect on Linkedin - www.linkedin.com/in/sourav-kumar-saikia
The following table contains all the problems with their respective solution tutorials. I will try to add more articles to it soon.
Problem Name | Problem No | Article Links |
---|---|---|
Contains Duplicate | 217 | https://sourav-saikia.medium.com/leetcode-217-contains-duplicate-solution-36f48793bce0 |
Jewels and Stones | 771 | https://sourav-saikia.medium.com/leetcode-771-jewels-and-stones-e35a368fc37 |
March LeetCoding Challenge
Problem Name | Problem No | Article Links |
---|---|---|
Distribute Candies | 575 | https://medium.com/dev-genius/march-leetcoding-challenge-2021-problem-1-distribute-candies-f37f66ea7ee9 |
Set Mismatch | 645 | https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-2-set-mismatch-4abd5ee491c9 |
Missing Number | 268 | https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-3-missing-number-ae8ee45a58cb |
Intersection of Two Linked Lists | 160 | https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-4-intersection-of-two-linked-lists-a775449b5563 |
Average of Levels in Binary Tree | 637 | https://medium.com/dev-genius/march-leetcoding-challenge-2021-day-5-average-of-levels-in-binary-tree-ac886b2b42e8 |
Short Encoding of Words | 820 | https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-6-short-encoding-of-words-7fed4bfae557 |
Remove Palindromic Subsequences | 1332 |
I have posted all the previous problems for the March LeetCoding Challenge 2021. You can check them out.
Top comments (0)