File tree Expand file tree Collapse file tree 1 file changed +12
-18
lines changed
Expand file tree Collapse file tree 1 file changed +12
-18
lines changed Original file line number Diff line number Diff line change 1- def longest_common_prefix (strs : list [str ])-> str :
1+ def longest_common_prefix (strs : list [str ]) -> str :
22 """
33 :type strs: List[str]
4-
4+
55 URL:
66 https://en.wikipedia.org/wiki/Longest_common_prefix
77
@@ -12,7 +12,7 @@ def longest_common_prefix(strs: list[str])->str:
1212 Returns:
1313 str:The longest common prefix shared among all strings.
1414 Returns an empty string if there is none.
15-
15+
1616 Test cases:
1717 >>> longest_common_prefix(["flower","flow","flap"])
1818 'fl'
@@ -27,25 +27,19 @@ def longest_common_prefix(strs: list[str])->str:
2727 >>> longest_common_prefix(["#hashtag","#hashbrown","#hash"])
2828 '#hash'
2929 """
30- ans = ""
30+ ans = ""
3131 if not strs :
3232 return ""
33- if len (strs )== 1 :
33+ if len (strs ) == 1 :
3434 return strs [0 ]
35-
36- k = 0
37- ref = strs [0 ]
3835
39- while k < len (ref ):
40- for ele in strs :
41- if len (ele )< k + 1 or ref [k ]!= ele [k ]:
36+ k = 0
37+ ref = strs [0 ]
4238
39+ while k < len (ref ):
40+ for ele in strs :
41+ if len (ele ) < k + 1 or ref [k ] != ele [k ]:
4342 return ans
44- ans += ele [k ]
45- k += 1
43+ ans += ele [k ]
44+ k += 1
4645 return ans
47-
48-
49-
50-
51-
You can’t perform that action at this time.
0 commit comments