Skip to content

Commit 8090375

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 125 Valid Palindrome
1 parent 15e63b3 commit 8090375

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
1212
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
1313
- [121 Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
1414
- [122 Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
15+
- [125 Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
1516
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
1617
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
1718

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
s = s.lower()
4+
left, right = 0, len(s) - 1
5+
while left < right:
6+
if (s[left] < "a" or s[left] > "z") and (s[left] < "0" or s[left] > "9"):
7+
left += 1
8+
elif (s[right] < "a" or s[right] > "z") and (
9+
s[right] < "0" or s[right] > "9"
10+
):
11+
right -= 1
12+
elif s[left] != s[right]:
13+
return False
14+
else:
15+
left += 1
16+
right -= 1
17+
return True

tests/test_125_valid_palindrome.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._125_valid_palindrome import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "expected"],
8+
argvalues=[
9+
("A man, a plan, a canal: Panama", True),
10+
("race a car", False),
11+
(" ", True),
12+
],
13+
)
14+
def test_func(s: str, expected: bool):
15+
max_profit = Solution().isPalindrome(s)
16+
assert max_profit == expected

0 commit comments

Comments
 (0)