Skip to content

Commit 394b46d

Browse files
committed
common_prefix
1 parent e718fb4 commit 394b46d

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ def find_longest_common_prefix(strings: List[str]):
77
88
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.
99
"""
10+
if len(strings) < 2:
11+
return ""
12+
13+
sorted_strings = sorted(strings)
1014
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
15+
16+
for i in range(len(sorted_strings) - 1):
17+
common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1])
18+
if len(common) > len(longest):
19+
longest = common
20+
1621
return longest
1722

1823

0 commit comments

Comments
 (0)