Greedy python solution that beats 97.66%


  • 0
    G
    class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) < 1: return ''
        elif len(strs) == 1: return strs[0]
        s = strs[0]
        for i in xrange(1, len(strs)):
            l = min(len(s), len(strs[i]))
            while strs[i][:l] != s[:l]: l -= 1
            s = s[:l]
        return s

Log in to reply
 

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