Simple and Quick Python Solution


  • 0
    Y
    class Solution(object):
        def longestWord(self, words):
            """
            :type words: List[str]
            :rtype: str
            """
            words_set = set(words)
            words_ls = sorted(words, key = lambda x: (-len(x), x))
            for w in words_ls:
                if all(x in words_set for x in [w[:i] for i in range(1, len(w))]):
                    return w
            return ''
    

Log in to reply
 

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