aboutsummaryrefslogtreecommitdiff
path: root/valid_palindrome2.py
diff options
context:
space:
mode:
Diffstat (limited to 'valid_palindrome2.py')
-rw-r--r--valid_palindrome2.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/valid_palindrome2.py b/valid_palindrome2.py
new file mode 100644
index 0000000..35d7298
--- /dev/null
+++ b/valid_palindrome2.py
@@ -0,0 +1,28 @@
+# 680. Valid Palindrome II
+
+"""
+given a string 's', return 'true' if the 's' can be palindrome after deleting
+at most one charater from it.
+"""
+
+
+class Solution(object):
+ def validPalindrome(self, s):
+ """
+ :type s: str
+ :rtype: bool
+ """
+ left, right = 0, len(s) - 1
+ while left < right:
+ if s[left] != s[right]:
+ one, two = s[left:right], s[left + 1 : right + 1]
+ return one == one[::-1] or two == two[::-1]
+ left, right = left + 1, right - 1
+ return True
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.validPalindrome("aba")) # expect: True
+ print(obj.validPalindrome("abca")) # expect: True
+ print(obj.validPalindrome("abc")) # expect: False