-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_common_prefix.py
33 lines (24 loc) · 1.06 KB
/
longest_common_prefix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Write a function to find the longest common prefix string amongst an array of strings.
# If there is no common prefix, return an empty string "".
from typing import List
from unittest import TestCase
def longestCommonPrefix(strs: List[str]) -> str:
if not strs:
return ''
strs.sort()
min_strs, max_strs = strs[0], strs[-1]
for i, letter in enumerate(min_strs):
if max_strs[i] != letter:
return min_strs[:i]
return min_strs
class TestsLongestCommonPrefix(TestCase):
def test_empty_list(self):
self.assertEqual('', longestCommonPrefix([]))
def test_with_one_element_in_list(self):
self.assertEqual('flower', longestCommonPrefix(["flower"]))
def test_simple_case(self):
self.assertEqual('fl', longestCommonPrefix(["flower", "flow", "flight"]))
def test_with_no_common_prefix(self):
self.assertEqual('', longestCommonPrefix(["dog", "racecar", "car"]))
def test_with_common_prefix(self):
self.assertEqual('a', longestCommonPrefix(["abcde", "abcdeea", "abc", "adbc"]))