-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlcp.py
More file actions
34 lines (30 loc) · 918 Bytes
/
lcp.py
File metadata and controls
34 lines (30 loc) · 918 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
31
32
33
34
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
# Length of any string would do as it a common prefix
if not strs:
return ""
if "" in strs:
return ""
n = len(strs[0])
# Pythonic with indexes
result = []
# Should be n+1 to include the total string
for i in range(n+1):
select = None
count = 0
for words in strs:
#print words[:i]
if select is None:
select = words[:i]
count += 1
else:
if words[:i] == select:
count += 1
if count == len(strs):
result.append(select)
print result
return result[-1]