Skip to content

Commit 8b6adb1

Browse files
committed
sort list to compare only neighbours
1 parent cf9e042 commit 8b6adb1

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ 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+
strings.sort()
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(strings) - 1):
17+
first_string = strings[i]
18+
second_string = strings[i + 1]
19+
20+
common = find_common_prefix(first_string, second_string)
21+
22+
if len(common) > len(longest):
23+
longest = common
24+
1625
return longest
1726

1827

0 commit comments

Comments
 (0)