Trie LeetCode Problems: All 49, With Python Solutions

Every problem in this library that LeetCode tags Trie 49 in total, 36 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 49 problems
  • 3 Easy
  • 24 Medium
  • 22 Hard

How Trie problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Trie problems — each one explains the approach, gives a Python template and states its complexity.

  • Trie — Store a set of words by their shared prefixes so lookups cost the length of the word.

Trie problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (3)

#ProblemDifficultyTopics
14Longest Common PrefixEasyTrie, Array, String
2932Maximum Strong Pair XOR IEasyBit Manipulation, Trie, Array +2
1065Index Pairs of a StringPremiumEasyTrie, Array, String +1

Medium (24)

#ProblemDifficultyTopics
139Word BreakMediumTrie, Memoization, Array +3
208Implement Trie (Prefix Tree)MediumDesign, Trie, Hash Table +1
211Design Add and Search Words Data StructureMediumDepth-First Search, Design, Trie +1
386Lexicographical NumbersMediumDepth-First Search, Trie
421Maximum XOR of Two Numbers in an ArrayMediumBit Manipulation, Trie, Array +1
648Replace WordsMediumTrie, Array, Hash Table +1
676Implement Magic DictionaryMediumDepth-First Search, Design, Trie +2
677Map Sum PairsMediumDesign, Trie, Hash Table +1
692Top K Frequent WordsMediumTrie, Array, Hash Table +5
720Longest Word in DictionaryMediumTrie, Array, Hash Table +2
792Number of Matching SubsequencesMediumTrie, Array, Hash Table +4
820Short Encoding of WordsMediumTrie, Array, Hash Table +1
1023Camelcase MatchingMediumTrie, Array, Two Pointers +2
1233Remove Sub-Folders from the FilesystemMediumDepth-First Search, Trie, Array +1
1268Search Suggestions SystemMediumTrie, Array, String +3
2261K Divisible Elements SubarraysMediumTrie, Array, Hash Table +3
2452Words Within Two Edits of DictionaryMediumTrie, Array, String
2707Extra Characters in a StringMediumTrie, Array, Hash Table +2
616Add Bold Tag in StringPremiumMediumTrie, Array, Hash Table +2
758Bold Words in StringPremiumMediumTrie, Array, Hash Table +2
1166Design File SystemPremiumMediumDesign, Trie, Hash Table +1
1698Number of Distinct Substrings in a StringPremiumMediumTrie, String, Suffix Array +2
1804Implement Trie II (Prefix Tree)PremiumMediumDesign, Trie, Hash Table +1
1858Longest Word With All PrefixesPremiumMediumDepth-First Search, Trie, Array +1

Hard (22)

#ProblemDifficultyTopics
140Word Break IIHardTrie, Memoization, Array +4
212Word Search IIHardTrie, Array, String +2
336Palindrome PairsHardTrie, Array, Hash Table +1
440K-th Smallest in Lexicographical OrderHardTrie
472Concatenated WordsHardDepth-First Search, Trie, Array +3
745Prefix and Suffix SearchHardDesign, Trie, Array +2
1032Stream of CharactersHardDesign, Trie, Array +2
1178Number of Valid Words for Each PuzzleHardBit Manipulation, Trie, Array +2
1316Distinct Echo SubstringsHardTrie, String, Hash Function +1
1707Maximum XOR With an Element From ArrayHardBit Manipulation, Trie, Array
1803Count Pairs With XOR in a RangeHardBit Manipulation, Trie, Array
1948Delete Duplicate Folders in SystemHardTrie, Array, Hash Table +2
2227Encrypt and Decrypt StringsHardDesign, Trie, Array +2
2416Sum of Prefix Scores of StringsHardTrie, Array, String +1
2935Maximum Strong Pair XOR IIHardBit Manipulation, Trie, Array +2
2977Minimum Cost to Convert String IIHardGraph, Trie, Array +3
425Word SquaresPremiumHardTrie, Array, String +1
527Word AbbreviationPremiumHardGreedy, Trie, Array +2
588Design In-Memory File SystemPremiumHardDesign, Trie, Hash Table +2
642Design Search Autocomplete SystemPremiumHardDepth-First Search, Design, Trie +4
1938Maximum Genetic Difference QueryHardBit Manipulation, Depth-First Search, Trie +2
2479Maximum XOR of Two Non-Overlapping SubtreesPremiumHardTree, Depth-First Search, Graph +1

When the Trie problem arrives live

Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.