14_Longest Common Prefix
Input: ["flower","flow","flight"]
Output: "fl"Input: ["dog","racecar","car"]
Output: ""Solution: vertical scanning
def longestCommonPrefix(strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs: return ''
out = []
i = 0
n = min([len(word) for word in strs])
for i in range(n):
l = list(strs[0])[i]
for j in range(1, len(strs)):
if list(strs[j])[i] != l:
return ''.join(out)
out.append(l)
return ''.join(out)Last updated