-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_prefix.py
More file actions
33 lines (29 loc) · 985 Bytes
/
common_prefix.py
File metadata and controls
33 lines (29 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def longestCommonPrefix(self, strs: list[str]) -> str:
min_index = 0
common_str = ""
if len(strs) == 0:
return ""
elif len(strs) == 1:
return strs[0]
else:
minimum = len(strs[0])
for i in range(len(strs) - 1):
l = len(strs[i])
s = strs[i]
if minimum > len(strs[i + 1]):
minimum = len(strs[i+1])
min_index = i + 1
if minimum == 0:
return ""
for i, letter in enumerate(strs[min_index]):
flag = True
for j in range(len(strs)):
if strs[j][i] != letter:
flag = False
if flag:
# if letter not in common_str:
common_str += letter
else:
return common_str
return common_str