File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed
Sprint-2/improve_with_precomputing/common_prefix Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 11from typing import List
22
33
4- def find_longest_common_prefix (strings : List [str ]) -> str :
4+ def find_longest_common_prefix (strings : List [str ]):
55 """
66 find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
77
@@ -16,13 +16,16 @@ def find_longest_common_prefix(strings: List[str]) -> str:
1616 longest = ""
1717
1818 for i in range (len (sorted_strings ) - 1 ):
19- s1 , s2 = sorted_strings [i ], sorted_strings [i + 1 ]
20- # Find common prefix between them
21- prefix_len = 0
22- max_len = min (len (s1 ), len (s2 ))
23- while prefix_len < max_len and s1 [prefix_len ] == s2 [prefix_len ]:
24- prefix_len += 1
25- if prefix_len > len (longest ):
26- longest = s1 [:prefix_len ]
19+ common = find_common_prefix (sorted_strings [i ], sorted_strings [i + 1 ])
20+ if len (common ) > len (longest ):
21+ longest = common
2722
2823 return longest
24+
25+
26+ def find_common_prefix (left : str , right : str ) -> str :
27+ min_length = min (len (left ), len (right ))
28+ for i in range (min_length ):
29+ if left [i ] != right [i ]:
30+ return left [:i ]
31+ return left [:min_length ]
You can’t perform that action at this time.
0 commit comments