Longest Common Prefix
Description:
Given k strings, find the longest common prefix (LCP).
Example
For strings “ABCD”, “ABEF” and “ACEF”, the LCP is “A”
For strings “ABCDEFG”, “ABCEFG” and “ABCEFA”, the LCP is “ABC”
Code:
class Solution:
"""
@param strs: A list of strings
@return: The longest common prefix
"""
def longestCommonPrefix(self, strs):
# write your code here
res = ""
if not strs:
return res
minL = 0
for i in strs:
if not i:
return res
maxL = min(minL, len(i))
for i in range(len(strs[0])):
equ = True
tmp = strs[0][i]
for j in range(len(strs)):
if strs[j][i]!=tmp:
equ = False
if not equ:
break
res += strs[0][i]
return res