-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem680.py
More file actions
23 lines (21 loc) · 778 Bytes
/
Problem680.py
File metadata and controls
23 lines (21 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def validPalindrome(self, s: str) -> bool:
# checking the string if its a palindrome
def check_palindrome(s, i, j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
i = 0
j = len(s) - 1
while i < j:
# found a mismatched pair, will try both deletions
if s[i] != s[j]:
return check_palindrome(s, i, j - 1) or check_palindrome(s, i + 1, j)
i += 1
j -= 1
return True
# if the imperfections are in the middle, then you can remove one to be a palindrome
# if one imperfection exists, everything should be equal below the bound