Skip to content

Commit 05c7c18

Browse files
authored
Merge pull request neetcode-gh#1469 from NN-Liu/main
Create 344-Reverse-String.py and Create 680-Valid-Palindrome-II.py
2 parents faeee81 + 2b2a2fe commit 05c7c18

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

python/344-Reverse-String.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def reverseString(self, s: List[str]) -> None:
3+
"""
4+
Do not return anything, modify s in-place instead.
5+
"""
6+
l = 0
7+
r = len(s) - 1
8+
while l <= r:
9+
s[l],s[r] = s[r],s[l]
10+
l += 1
11+
r -= 1

python/680-Valid-Palindrome-II.py

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

0 commit comments

Comments
 (0)