14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

 

 

Note:

All given inputs are in lowercase letters a-z.

 

class Solution(object):
# @return a string
    def longestCommonPrefix(self, strs):
        if not strs:
            return ""
            
        for i, letter_group in enumerate(zip(*strs)):
            if len(set(letter_group)) > 1:
                return strs[0][:i]
        else:
            return min(strs)

 

posted on 2018-08-26 17:22  jydd  阅读(61)  评论(0编辑  收藏  举报

导航