A Python version with 44ms


  • 0
    C
    class Solution:
        # @return a string
        def longestCommonPrefix(self, strs):
            
            if not strs:
                return ''
            
            result = strs[0]
            for s in strs[1:]:
                if result == '':
                    break
                
                while result != '' :
                    if self.matchstr(result, s):
                        break
                    else:
                        result = result[:-1]
                        
            return result
        
        def matchstr(self, common, s):
            if len(s) < len(common):
                return False
            else:
                slen = len(common)
                return common[:slen] == s[:slen]

Log in to reply
 

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