-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-common-prefix.py
124 lines (108 loc) · 3.25 KB
/
longest-common-prefix.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
"""
# V0
class Solution:
def longestCommonPrefix(self, strs):
if not strs:
return ""
r = ""
# NOTE : in 1st loop, we loop element in 1st str
for i in range(len(strs[0])):
# NOTE : in 2nd loop, we loop sub-str in strs
for j in range(1, len(strs)):
# NOTE : i >= len(strs[j]), meaning if index in 1st sub-str already bigger than the next sub-str, so there is no possible `longest prefix` anymore, then we return current result
if i >= len(strs[j]) or strs[0][i] != strs[j][i]:
return r
r += strs[0][i]
return r
# V1
# https://blog.csdn.net/coder_orz/article/details/51706442
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ''
res = ''
for i in range(len(strs[0])):
for j in range(1, len(strs)):
if i >= len(strs[j]) or strs[j][i] != strs[0][i]:
return res
res += strs[0][i]
return res
# V1'
# https://blog.csdn.net/coder_orz/article/details/51706442
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ''
strs.sort()
res = ''
for i in range(len(strs[0])):
if i >= len(strs[-1]) or strs[-1][i] != strs[0][i]:
return res
res += strs[0][i]
return res
# V1''
# https://blog.csdn.net/coder_orz/article/details/51706442
# IDEA : ZIP PYTHON DEFAULT FUNC
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ''
for i, chars in enumerate(zip(*strs)):
if len(set(chars)) > 1:
return strs[0][:i]
return min(strs)
# V2
# Time: O(n * k), k is the length of the common prefix
# Space: O(1)
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
for i in range(len(strs[0])):
for string in strs[1:]:
if i >= len(string) or string[i] != strs[0][i]:
return strs[0][:i]
return strs[0]
# Time: O(n * k), k is the length of the common prefix
# Space: O(k)
class Solution2(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
prefix = ""
for chars in zip(*strs):
if all(c == chars[0] for c in chars):
prefix += chars[0]
else:
return prefix
return prefix