Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1469 from NN-Liu/main
Browse files Browse the repository at this point in the history
Create 344-Reverse-String.py and Create 680-Valid-Palindrome-II.py
  • Loading branch information
Ahmad-A0 authored Nov 19, 2022
2 parents faeee81 + 2b2a2fe commit 05c7c18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/344-Reverse-String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
l = 0
r = len(s) - 1
while l <= r:
s[l],s[r] = s[r],s[l]
l += 1
r -= 1
21 changes: 21 additions & 0 deletions python/680-Valid-Palindrome-II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def validPalindrome(self, s: str) -> bool:
i, j = 0, len(s) - 1

while i < j:
if s[i] == s[j]:
i += 1
j -= 1
else:
return self.validPalindromeUtil(s, i + 1, j) or self.validPalindromeUtil(s, i, j - 1)
return True

def validPalindromeUtil(self, s, i, j):
while i < j:
if s[i] == s[j]:
i += 1
j -= 1
else:
return False

return True

0 comments on commit 05c7c18

Please sign in to comment.