[Python] no zip no enumerate

  • 0

    First find out the shortest word, then check if they all have the first char ,the second char, and so on...

    def longestCommonPrefix(self, strs):
            :type strs: List[str]
            :rtype: str
            if strs is None or len(strs) < 1: return ''
            maxlen = len(strs[0])
            for s in strs:
                if len(s) < maxlen:
                    maxlen = len(s)
            for i in range(maxlen):
                if not reduce(lambda x, y: x and y, (cr[i]==strs[0][i] for cr in strs)):
                    return strs[0][:i]
            return strs[0][:maxlen]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.