Skip to content

Commit 3d6513a

Browse files
common prefix
1 parent e718fb4 commit 3d6513a

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33

44
def find_longest_common_prefix(strings: List[str]):
5-
"""
6-
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
7-
8-
In the event that an empty list, a list containing one string, or a list of strings with no common prefixes is passed, the empty string will be returned.
9-
"""
5+
if len(strings) < 2:
6+
return ""
7+
sorted_strings = sorted(strings)
108
longest = ""
11-
for string_index, string in enumerate(strings):
12-
for other_string in strings[string_index+1:]:
13-
common = find_common_prefix(string, other_string)
14-
if len(common) > len(longest):
15-
longest = common
9+
for i in range(len(sorted_strings) - 1):
10+
common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1])
11+
if len(common) > len(longest):
12+
longest = common
13+
1614
return longest
1715

1816

0 commit comments

Comments
 (0)