Skip to content

Commit 03fbf9f

Browse files
authored
Create Valid Palindrome II.py
1 parent 20dec65 commit 03fbf9f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Valid Palindrome II.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
3+
4+
Example 1:
5+
6+
Input: "aba"
7+
Output: True
8+
9+
Example 2:
10+
11+
Input: "abca"
12+
Output: True
13+
Explanation: You could delete the character 'c'.
14+
15+
Note:
16+
17+
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
18+
19+
'''
20+
21+
class Solution(object):
22+
def validPalindrome(self, s):
23+
"""
24+
:type s: str
25+
:rtype: bool
26+
"""
27+
vec = list(s)
28+
29+
left = 0
30+
right = len(vec) - 1
31+
while left < right:
32+
if s[left] != s[right]:
33+
return self.valid(s[left:right]) or self.valid(s[left+1:right+1])
34+
left += 1
35+
right -= 1
36+
37+
return True
38+
39+
def valid(self, vec):
40+
n = len(vec)
41+
for i in xrange(n // 2):
42+
if vec[i] != vec[n-i-1]:
43+
return False
44+
return True

0 commit comments

Comments
 (0)