Skip to content

Commit dc097fe

Browse files
committed
Use find_common_prefix helper as suggested
1 parent 90d768e commit dc097fe

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List
22

33

4-
def find_longest_common_prefix(strings: List[str]) -> str:
4+
def find_longest_common_prefix(strings: List[str]):
55
"""
66
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
77
@@ -16,13 +16,16 @@ def find_longest_common_prefix(strings: List[str]) -> str:
1616
longest = ""
1717

1818
for i in range(len(sorted_strings) - 1):
19-
s1, s2 = sorted_strings[i], sorted_strings[i + 1]
20-
# Find common prefix between them
21-
prefix_len = 0
22-
max_len = min(len(s1), len(s2))
23-
while prefix_len < max_len and s1[prefix_len] == s2[prefix_len]:
24-
prefix_len += 1
25-
if prefix_len > len(longest):
26-
longest = s1[:prefix_len]
19+
common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1])
20+
if len(common) > len(longest):
21+
longest = common
2722

2823
return longest
24+
25+
26+
def find_common_prefix(left: str, right: str) -> str:
27+
min_length = min(len(left), len(right))
28+
for i in range(min_length):
29+
if left[i] != right[i]:
30+
return left[:i]
31+
return left[:min_length]

0 commit comments

Comments
 (0)