Python 52-62 ms solution


  • 0
    D

    class Solution(object):
    def longestCommonPrefix(self, strs):
    """
    :type strs: List[str]
    :rtype: str
    """
    prefix = ''

        for i in xrange(len(strs)):
            if i == 0:
                prefix = strs[i]
            else:
                if strs[i] == '':
                    prefix = ''
                for ind in xrange(len(strs[i])):
                    if len(prefix) <= ind:
                        pass
                    elif strs[i][ind] != prefix[ind]:
                        prefix = prefix[:ind]
                    elif ind == len(strs[i]) - 1:
                        prefix = prefix[:ind + 1]
        return prefix

Log in to reply
 

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