Skip to content

Commit 8276f08

Browse files
committed
addin algo
1 parent 46b96b6 commit 8276f08

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-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+
4+
class Solution:
5+
def longestCommonPrefix(self, strs: List[str]) -> str:
6+
7+
if not strs:
8+
return ''
9+
else:
10+
min_strs, max_strs = min(strs), max(strs)
11+
count = 0
12+
13+
len_min_strs = len(min_strs)
14+
15+
for i in range(len_min_strs):
16+
if min_strs[i] != max_strs[i]:
17+
break
18+
else:
19+
count += 1
20+
21+
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_15\
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)