One line solution using itertools.takewhile


  • 1
    T
    def longestCommonPrefix(self, strs):
        return reduce(lambda s1, s2: ''.join(y[0] for y in itertools.takewhile(lambda x: x[0] == x[1], zip(s1, s2))), strs or [''])

Log in to reply
 

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