Python 40ms beats 95%


  • 1
    class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0:
            return ""
        
        minLen = len(strs[0])
        for i in range(len(strs)):
            minLen = min(len(strs[i]), minLen)
            
        lcp = ""
        i = 0
        while i < minLen:
            char = strs[0][i]
            for j in xrange(1, len(strs)):
                if strs[j][i] != char:
                    return lcp
            lcp = lcp + char
            i += 1
            
        return lcp

Log in to reply
 

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