Skip to content

Commit f34a6a4

Browse files
authored
Merge pull request #1442 from ivan1016017/november01
adding algo
2 parents 850b057 + 1d6ae81 commit f34a6a4

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Check if it is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
if s[i] != s[len_s - 1 -i]:
19+
return False
20+
21+
return True
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def longestCommonPrefix(self, strs: List[str]) -> str:
6+
7+
if not strs:
8+
return ''
9+
10+
min_strs, max_strs = min(strs), max(strs)
11+
count = 0
12+
13+
for i in range(len(min_strs)):
14+
15+
if min_strs[i] == max_strs[i]:
16+
count +=1
17+
else:
18+
break
19+
20+
return min_strs[:count]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.longest_common_prefix import Solution
4+
5+
class LongestCommonPrefixTestCase(unittest.TestCase):
6+
7+
def test_longest_common_prefix(self):
8+
solution = Solution()
9+
output = solution.longestCommonPrefix(strs=["flower","flow","flight"])
10+
target = 'fl'
11+
self.assertEqual(target, output)
12+
13+
def test_longest_no_common_prefix(self):
14+
solution = Solution()
15+
output = solution.longestCommonPrefix(strs=["dog","racecar","car"])
16+
target = ''
17+
self.assertEqual(target, output)
18+
19+
def test_longest_common_prefix_null_list(self):
20+
solution = Solution()
21+
output = solution.longestCommonPrefix(strs=[])
22+
target = ''
23+
self.assertEqual(target, output)

0 commit comments

Comments
 (0)