Skip to content

Commit 7dbdbf0

Browse files
authored
Create Valid-Palindrome-II.py
1 parent 1395017 commit 7dbdbf0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Leetcode/Valid-Palindrome-II.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def validPalindrome(self, s: str) -> bool:
3+
if len(s) == 0:
4+
return True
5+
if s == s[::-1]:
6+
return True
7+
8+
def is_palin(s, start, tail):
9+
while start < tail:
10+
if s[start] != s[tail]:
11+
return False
12+
start += 1
13+
tail -= 1
14+
return True
15+
16+
start = 0
17+
tail = len(s) - 1
18+
while start < tail:
19+
if s[start] != s[tail]:
20+
return is_palin(s, start + 1, tail) or is_palin(s, start, tail - 1)
21+
start += 1
22+
tail -= 1
23+
return True
24+
25+

0 commit comments

Comments
 (0)