Skip to content

Commit b4e7e9a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a7fb739 commit b4e7e9a

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

strings/longest_common_prefix.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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-

0 commit comments

Comments
 (0)