Skip to content

Commit 8c4178b

Browse files
committed
9.22
1 parent 269b8f0 commit 8c4178b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

78.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
@param strs: A list of strings
4+
@return: The longest common prefix
5+
"""
6+
def longestCommonPrefix(self, strs):
7+
# write your code here
8+
if len(strs) == 0:
9+
return ""
10+
elif len(strs) == 1:
11+
return strs[0]
12+
res = strs[0]
13+
for string in strs:
14+
if string == "":
15+
return ""
16+
for char_index in range(len(string)):
17+
if char_index >= len(res):
18+
break
19+
if res[char_index] != string[char_index]:
20+
res = string[0:char_index]
21+
break
22+
return res
23+
24+
A = ["ABCD", "ABCEF", "ABCEDF"]
25+
print(Solution.longestCommonPrefix(A, A))

0 commit comments

Comments
 (0)