My python implementation with only len() function


  • 0
    T
    class Solution(object):
        def longestCommonPrefix(self, strs):
            """
            :type strs: List[str]
            :rtype: str
            """
    
            if len(strs) == 0: return ''
            len_prefix = 0
            max_prefix = ''
            isStoped = False
            while len(strs[0]) > len_prefix:
            	char = strs[0][len_prefix]
            	for i in xrange(1,len(strs)):
            		if (len(strs[i]) <= len_prefix) or (strs[i][len_prefix] != char):
            			isStoped = True
            			break
            	if isStoped:
            		break
            	else:
            		len_prefix += 1
            		max_prefix += char
            return max_prefix

Log in to reply
 

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