Skip to content

Commit 863555a

Browse files
authored
Optimize common prefix function
using sorted(strings) to prevent the side effect.
1 parent 01e2c72 commit 863555a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def find_longest_common_prefix(strings: List[str]):
1010
if len(strings) < 2:
1111
return ""
1212
# reducing comparison by sorting places strings with similar prefixes next to each other...
13-
strings.sort()
13+
sorted_strings = sorted(strings)
1414

1515
longest = ""
16-
for i in range(len(strings) - 1):
16+
for i in range(len(sorted_strings) - 1):
1717
# compares only adjacent strings after sorting
18-
common = find_common_prefix(strings[i], strings[i + 1])
18+
common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1])
1919
if len(common) > len(longest):
2020
longest = common
2121
return longest

0 commit comments

Comments
 (0)