leetcode 14-> Longest Common Prefix

note:All given inputs are in lowercase letters a-z.

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs)==0: return ''
        strs = sorted(strs, key=lambda x:len(x))
        keyWord = strs[0]
        sharedSt = ''
        charPos = 0 
        for c in keyWord:
            if all(c==strs[ii][charPos] for ii in range(1, len(strs))):
                charPos+=1
                sharedSt+=c
            else:
                return sharedSt
        return  sharedSt

原文地址:https://www.cnblogs.com/sea-stream/p/10515033.html