forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-palindrome-ii.py
35 lines (32 loc) · 999 Bytes
/
valid-palindrome-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Time: O(n)
# Soace: O(1)
# Given a non-empty string s, you may delete at most one character.
# Judge whether you can make it a palindrome.
#
# Example 1:
# Input: "aba"
# Output: True
# Example 2:
# Input: "abca"
# Output: True
# Explanation: You could delete the character 'c'.
# Note:
# The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
def validPalindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left, right = left+1, right-1
return True
left, right = 0, len(s)-1
while left < right:
if s[left] != s[right]:
return validPalindrome(s, left, right-1) or validPalindrome(s, left+1, right)
left, right = left+1, right-1
return True