Comparing one position for all strings at one time


  • 0
    B
    class Solution:
    # @return a string
    def longestCommonPrefix(self, strs):
        if len(strs)==0:
            return ''
        answer=[]
        j=0
        while True:
            if j>len(strs[0])-1:
                break
            temp=strs[0][j]
            for i in range(1,len(strs)):
                if j>len(strs[i])-1 or temp!=strs[i][j]:
                    temp=''
                    break
            if temp=='':
                break
            answer.append(temp)
            j=j+1
            
        return ''.join(answer)

Log in to reply
 

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