Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:

if not strs:
return ''
else:
min_strs, max_strs = min(strs), max(strs)
count = 0

for i in range(len(min_strs)):
if min_strs[i] != max_strs[i]:
break
else:
count += 1

return min_strs[:count]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
from src.my_project.interviews.top_150_questions_round_12\
.longest_common_prefix import Solution

class LongestCommonPrefixTestCase(unittest.TestCase):

def test_longest_common_prefix(self):
solution = Solution()
output = solution.longestCommonPrefix(strs=["flower","flow","flight"])
target = 'fl'
self.assertEqual(target, output)

def test_longest_no_common_prefix(self):
solution = Solution()
output = solution.longestCommonPrefix(strs=["dog","racecar","car"])
target = ''
self.assertEqual(target, output)

def test_longest_common_prefix_null_list(self):
solution = Solution()
output = solution.longestCommonPrefix(strs=[])
target = ''
self.assertEqual(target, output)
Loading