Skip to content

Commit f807d7a

Browse files
committed
adding valid palindrome
1 parent c9ddbb8 commit f807d7a

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# transform to lower case
9+
s = s.lower()
10+
11+
# remove non-alphanumerical value in the string
12+
s = re.sub(pattern='[^a-zA-Z0-9]',
13+
repl='',
14+
string=s)
15+
16+
len_s = len(s)
17+
18+
for i in range(len_s//2):
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_14\
3+
.valid_palindrome import Solution
4+
5+
class ValidPalindromeTestCase(unittest.TestCase):
6+
7+
def test_is_valid_palindrome(self):
8+
solution = Solution()
9+
output = solution.isPalindrome(s="A man, a plan, a canal: Panama")
10+
self.assertTrue(output)
11+
12+
def test_is_no_valid_palindrome(self):
13+
solution = Solution()
14+
output = solution.isPalindrome(s="race a car")
15+
self.assertFalse(output)

0 commit comments

Comments
 (0)