Sharing my accepted 75ms Python solution.


  • 0
    X
    class Solution:
        # @return a string
        def longestCommonPrefix(self, strs):
            if strs == []:
                return ""
            prefix = strs[0]
            for s in strs[1:]:
                less = min(len(prefix), len(s))
                prefix = prefix[:less]
                for i in range(less):
                    if prefix[i] != s[i]:
                        prefix = prefix[:i]
                        break
            return prefix

Log in to reply
 

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