Here is mine python code ,but does it have a better runtime?

  • 2

    I think the runtime is O(n*m).There is any better way? Tks

    class Solution:
    # @return a string
    def longestCommonPrefix(self, strs):
        if len(strs) == 0 :
            return ""
        for idx in range(1,len(strs)):
            endid = 0
            for c in pre:
                if len(strs[idx]) > endid and c == strs[idx][endid]:
                    endid = endid +1
                else :
            if endid == 0:
                return ""
            pre = pre[0:endid]
        return pre

  • 0

    for loop may be reversed.

Log in to reply

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