Created
May 14, 2020 19:59
-
-
Save c02y/cb758d663750a4c227d3a26c70f275c0 to your computer and use it in GitHub Desktop.
ValidPalindromeII.py
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
# 680. Valid Palindrome II (Easy) | |
# https://leetcode-cn.com/problems/valid-palindrome-ii/description/ | |
class Solution: | |
def validPalindrome(self, s): | |
if s == s[::-1]: | |
return True | |
l, r = 0, len(s) - 1 | |
while l < r: | |
if s[l] == s[r]: | |
l, r = l + 1, r - 1 | |
else: | |
# NOTE: l:r does not include r | |
a = s[l + 1:r + 1] | |
b = s[l:r] | |
return a == a[::-1] or b == b[::-1] | |
if __name__ == '__main__': | |
mystring = "abcdedcxba" | |
if Solution().validPalindrome(mystring): | |
print("True") | |
else: | |
print("False") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cpp version: