Python concise code 42 ms

  • 0

    The time complexity is O(min_length * n), where min_length is the minimum length of the strs and n is the number of strs.

    class Solution(object):
        def longestCommonPrefix(self, strs):
            :type strs: List[str]
            :rtype: str
            if not strs:
                return ''
            min_length = min(len(s) for s in strs)
            prefix = ''
            for i in range(min_length):
                c = strs[0][i]
                for s in strs:
                    if s[i] != c:
                        return prefix
                prefix += c
            return prefix

Log in to reply

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