14.Longest Common Prefix

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        
        if len(strs) == 0:
            return ""
        last_s = strs[0]
        same_prefix = ""
        
        for i, s in enumerate(strs):
            if len(last_s) > len(s):
                c = last_s
                last_s = s
                s = c
            for j, w in enumerate(last_s):
                if s[j] == w:
                    same_prefix += w
                else:
                    last_s = same_prefix
                    same_prefix = ""
                    break
                if j+1 == len(last_s):
                    last_s = same_prefix
                    same_prefix = ""
        return last_s
原文地址:https://www.cnblogs.com/luo-c/p/12857138.html