-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode Problem 680 Valid Palindrome II.txt
46 lines (38 loc) · 1.29 KB
/
Leetcode Problem 680 Valid Palindrome II.txt
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
36
37
38
39
40
41
42
43
44
45
46
680. Valid Palindrome II
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:
def validPalindrome(self, s: str) -> bool:
start = 0
end = len(s) - 1
mismatchLeft = 0
mismatchRight = 0
mismatchCounter = 0
while(start < end):
if s[start] != s[end]:
#Ignore the char from left and record the positions
if mismatchCounter == 0:
mismatchLeft = start
mismatchRight = end
start += 1
mismatchCounter += 1
#Go back to previous mismatch and try ignoring from right
elif mismatchCounter == 1:
start = mismatchLeft
end = mismatchRight
end -= 1
mismatchCounter += 1
else:
return False
else:
start += 1
end-= 1
return True