Simple python solution with 51ms


  • 0
    S
    class Solution(object):
        def longestCommonPrefix(self, strs):
            if not strs: return ""
            if len(strs)==1: return strs[0]
            s, i= "", 0
            for x in strs[0]:
                for j in xrange(1,len(strs)):
                    if i>=len(strs[j]) or x!= strs[j][i]:
                        return s
                s, i= s+x, i+1
            return s

Log in to reply
 

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