-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-common-prefix.py
executable file
·51 lines (41 loc) · 1.24 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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 17 14:38:28 2020
@author: johnoyegbite
"""
# SOLVED!
def longestCommonPrefix(strs):
"""
:type strs: List[str]
:rtype: str
"""
if not len(strs):
return ""
if len(strs) <= 1:
return strs[-1]
done = False
major_prefix = ""
longest = [major_prefix]
for i in range(len(strs[0])):
prefix = strs[0][i]
for j in range(1, len(strs)):
if i < len(strs[j]) and strs[j][i] == prefix and j < len(strs)-1:
continue
elif i < len(strs[j]) and strs[j][i] == prefix and j < len(strs):
major_prefix += prefix
longest.append(major_prefix)
else:
major_prefix = ""
done = True
break
if done:
break
return longest[-1]
if __name__ == "__main__":
strs = ["flower", "flower", "flightwer", "fleewer", "floorwer"] # fl
# strs = ["dog","racecar","car"] # ""
strs = ['flower','glow','floor'] # ""
# strs = ["aca","cba"] ""
# strs = [] # ""
# strs = ["race"] # "race"
print(longestCommonPrefix(strs))