Share my accepted Python solution


  • 1
    M
    class Solution(object):
        def longestCommonPrefix(self, strs):
            """
            :type strs: List[str]
            :rtype: str
            """
            if len(strs) == 0:
                return ""
            else:
                res = ""
                b = [len(i) for i in strs]
                MinL = reduce(lambda x, y: min(x, y), b)
                for j in xrange(MinL):
                    head = map(lambda x: x[j], strs)
                    if len(set(head)) != 1:
                        break
                    else:
                        res += head[0]
               return res

  • 0
    H

    Why not MinL = min(b) instead of MinL = reduce(lambda x, y: min(x, y), b)?


Log in to reply
 

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