forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d32191
commit ace2c6a
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
''' | ||
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: | ||
Input: s = "ababa" | ||
Output: 1 | ||
Explanation: String is already palindrome | ||
Example: | ||
Input: s = "abb" | ||
Output: 2 | ||
Explanation: "abb" -> "bb" -> "". | ||
Remove palindromic subsequence "a" then "bb". | ||
Example: | ||
Input: s = "baabb" | ||
Output: 2 | ||
Explanation: "baabb" -> "b" -> "". | ||
Remove palindromic subsequence "baab" then "b". | ||
Example: | ||
Input: s = "" | ||
Output: 0 | ||
Constraints: | ||
- 0 <= s.length <= 1000 | ||
- s only consists of letters 'a' and 'b' | ||
''' | ||
#Difficulty: Easy | ||
#49 / 49 test cases passed. | ||
#Runtime: 32 ms | ||
#Memory Usage: 14.1 MB | ||
|
||
#Runtime: 32 ms, faster than 58.63% of Python3 online submissions for Remove Palindromic Subsequences. | ||
#Memory Usage: 14.1 MB, less than 90.00% of Python3 online submissions for Remove Palindromic Subsequences. | ||
|
||
class Solution: | ||
def removePalindromeSub(self, s: str) -> int: | ||
if not s: | ||
return 0 | ||
elif s == s[::-1]: | ||
return 1 | ||
else: | ||
return 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters