keep track of length of common substring, add 1 each time


  • 0
    A
    class Solution(object):
        def longestCommonPrefix(self, strs):
        	if len(strs) == 0:
        		return ""
        	n = 0
        	ans = True 
        	while ans:
        		if len(strs[0]) < n:
        			break
        		s = strs[0][0:n]
        		for i in xrange(0, len(strs)):
        			
        			if strs[i][0:n] != s:
        				ans = False
        				break 
        				  
        		if ans == True:
        			n += 1
    
        	if n == 0:
        		return ""
        	else:
        		return strs[0][0:n-1]
    
    

Log in to reply
 

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