Skip to content

Commit 045f7c4

Browse files
Merge pull request #4 from codewithme-py/feat/longest-common-prefix-0014
problem solution & test #14
2 parents caa79e2 + dab3f52 commit 045f7c4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def longestCommonPrefix(self, strs: list[str]) -> str:
3+
"""Find the longest common prefix string
4+
amongst an array of strings.
5+
"""
6+
if not strs:
7+
return ""
8+
9+
prefix = strs[0]
10+
for s in strs[1:]:
11+
while not s.startswith(prefix):
12+
prefix = prefix[:-1]
13+
if not prefix:
14+
return ""
15+
return prefix
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from solutions.longest_common_prefix_0014 import Solution
2+
3+
4+
def test_longest_common_prefix():
5+
sol = Solution()
6+
assert sol.longestCommonPrefix(["flower", "flow", "flight"]) == "fl"
7+
assert sol.longestCommonPrefix(["dog", "racecar", "car"]) == ""
8+
assert sol.longestCommonPrefix([]) == ""
9+
assert sol.longestCommonPrefix(["hello"]) == "hello"
10+
assert sol.longestCommonPrefix(["test", "test", "test"]) == "test"
11+
assert sol.longestCommonPrefix(["a", "b"]) == ""
12+
assert sol.longestCommonPrefix(["", "abc"]) == ""

0 commit comments

Comments
 (0)