Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
TrieTrees | Hash MapHashing | |
|---|---|---|
| Use case | Prefix queries, autocomplete, lexicographic enumeration, pruning multi-word grid search. | Exact-match membership and counting of whole strings. |
| Requirements | A node per character with child pointers or a map; a terminal flag. | Hashing the whole string (O(L) per hash). |
| Time complexity | O(L) insert/search/prefix for a word of length L, independent of dictionary size. | Expected O(L) per operation (hash computation), O(1) bucket work. |
| Space complexity | O(total characters * alphabet) in the worst case; shared prefixes reduce it. | O(total characters). |
| Strengths | Prefix operations are native; sorted traversal falls out of DFS; supports wildcard and XOR-maximization variants. | Minimal code; fastest exact lookups; built into every language. |
| Weaknesses | Heavy memory per node; slower than hashing for exact lookups; more code. | No prefix queries without inserting every prefix; no ordered iteration. |
| Example problems | Implement trie, word search II, design add-and-search words, maximum XOR of two numbers. | Group anagrams, word break (set of words), longest word in dictionary. |
| Choose this when | Choose a trie when queries are about prefixes, or when a DFS over characters must stop early on dead prefixes. | Choose a hash map/set when you only ever ask "is this exact string present" or count whole strings. |