-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_Longest_Common_Prefix.py
36 lines (30 loc) · 1.08 KB
/
14_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
34
35
36
from typing import List
import pytest
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
res = ""
for j in range(len(strs[0])):
for i in range(len(strs) - 1):
if len(strs[i + 1]) < j + 1:
return res
if strs[i][j] == strs[i + 1][j]:
continue
else:
return res
res += strs[0][j]
return res
@pytest.mark.parametrize("test_input,expected", [
(["flower", "flow", "flight"], "fl"),
(["dog", "racecar", "car"], ""),
([""], ""),
(["dog"], "dog"),
(["veryLongTextveryLongTextveryLongTextveryLongTxtveryLongText", ""], ""),
(["objectifying", "objectifying", "objectifying"], "objectifying"),
(["dog", "racecar", "car", ""], ""),
(["just", "justification", "justify"], "just"),
(["dog", "door", "doom"], "do"),
(["iterator", "italic", "item"], "it")
])
def test_longest_common_prefix(test_input, expected):
solution = Solution()
assert solution.longestCommonPrefix(test_input) == expected