-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestCommonPrefix.py
41 lines (34 loc) · 975 Bytes
/
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
36
37
38
39
40
41
'''
https://leetcode.com/problems/longest-common-prefix/
'''
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ""
elif len(strs) == 1:
return strs[0]
i = 0 # Go through characters in a word
j = 0 # Go through list of strings
output = ''
if len(strs[0]) != 0:
ref = strs[0][i]
else:
return output
while i <= len(strs[0]):
for j in strs:
if i < len(j):
if ref != j[i]:
return output
else:
return output
output = str(output) + str(ref)
i += 1
if i < len(strs[0]):
ref = strs[0][i]
else:
break
return output