Skip to content

Commit bda8582

Browse files
committed
improved common prefix
1 parent 134e683 commit bda8582

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ 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+
strings_sorted = sorted(strings)
1011
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
12+
for i in range(1, len(strings_sorted)):
13+
string = strings_sorted[i - 1]
14+
other_string = strings_sorted[i]
15+
common = find_common_prefix(string, other_string)
16+
if len(common) > len(longest):
17+
longest = common
1618
return longest
1719

1820

0 commit comments

Comments
 (0)