A Python version with 48ms


  • 0
    C
    class Solution:
        def longestCommonPrefix(self, strs):
            if len(strs) == 0:
                return ''
    
            lcp = strs[0]
            for str in strs[1:]:
                if lcp == '':
                    break
                
                lcp = self.getLCP(lcp, str)
            return lcp
    
        def getLCP(self, s1, s2):
            count = 0
            for i in xrange(min(len(s1), len(s2))):
                count = i + 1
                if s1[i] != s2[i]:
                    count -= 1
                    break
            return s1[:count]

Log in to reply
 

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