You guys are too sloppy on "elegance" of code. Python solution presented


  • 0
    B
    class Solution:
        # @return a string
        def longestCommonPrefix(self, strs):
            trie = {}
            for string in strs:
                current = trie
                for x in string:
                    if x not in current:
                        current[x] = {}
                    current = current[x]
                # using '' represents end of string
                current[''] = {}
            prefix = []
            while len(trie) == 1:
                char = next(trie.iterkeys())
                prefix.append(char)
                trie = trie[char]
            return ''.join(prefix)

Log in to reply
 

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