-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.LongestCommonPrefix.py
35 lines (29 loc) · 994 Bytes
/
14.LongestCommonPrefix.py
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
31
32
33
34
35
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
prefix = strs[0]
i = 1
while i < len(strs) and len(prefix) > 0:
if strs[i] == "":
return ""
if strs[i][0] != prefix[0]:
return ""
full_word = True
process_len = min(len(prefix), len(strs[i]))
for j in range(process_len):
if prefix[j] != strs[i][j]:
full_word = False
prefix = prefix[:j]
break
if j == len(strs[i]) - 1 and full_word:
prefix = strs[i]
i += 1
return prefix
# nice Idea:
# if not S: return ''
# m, M, i = min(S), max(S), 0
# for i in range(min(len(m),len(M))):
# if m[i] != M[i]: break
# else: i += 1
# return m[:i]