Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 1.39 KB

_720. Longest Word in Dictionary.md

File metadata and controls

53 lines (40 loc) · 1.39 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 27, 2024

Last updated : June 27, 2024


Related Topics : Array, Hash Table, String, Trie, Sorting

Acceptance Rate : 52.83 %


Solutions

Python

class Solution:
    def longestWord(self, words: List[str]) -> str:
        words.sort(key=lambda x: len(x))

        trie = {}
        maxx = 0
        maxxWord = ['']
        for word in words :
            curr = trie
            for i, c in enumerate(word) :
                if i == len(word) - 1 :
                    if maxx < len(word) :
                        maxx = len(word)
                        maxxWord = [word]
                    elif maxx == len(word) :
                        maxxWord.append(word)
                    
                    curr[c] = {}
                    break
                if c in curr :
                    curr = curr[c]
                else :
                    break
            
        return sorted(maxxWord)[0]