Hash Table LeetCode Problems: All 588, With Python Solutions

Every problem in this library that LeetCode tags Hash Table 588 in total, 492 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 588 problems
  • 143 Easy
  • 347 Medium
  • 98 Hard

How Hash Table problems are solved

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

  • Hash Map — Trade memory for time: remember what you have seen so the second pass never happens.

Hash Table problems by difficulty

Showing the first 200 of 588 problems. Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (47)

#ProblemDifficultyTopics
1Two SumEasyArray, Hash Table
13Roman to IntegerEasyHash Table, Math, String
141Linked List CycleEasyHash Table, Linked List, Two Pointers
160Intersection of Two Linked ListsEasyHash Table, Linked List, Two Pointers
169Majority ElementEasyArray, Hash Table, Divide and Conquer +2
202Happy NumberEasyHash Table, Math, Two Pointers
205Isomorphic StringsEasyHash Table, String
217Contains DuplicateEasyArray, Hash Table, Sorting
219Contains Duplicate IIEasyArray, Hash Table, Sliding Window
242Valid AnagramEasyHash Table, String, Sorting
268Missing NumberEasyBit Manipulation, Array, Hash Table +3
290Word PatternEasyHash Table, String
349Intersection of Two ArraysEasyArray, Hash Table, Two Pointers +2
350Intersection of Two Arrays IIEasyArray, Hash Table, Two Pointers +2
383Ransom NoteEasyHash Table, String, Counting
387First Unique Character in a StringEasyQueue, Hash Table, String +1
389Find the DifferenceEasyBit Manipulation, Hash Table, String +1
409Longest PalindromeEasyGreedy, Hash Table, String
448Find All Numbers Disappeared in an ArrayEasyArray, Hash Table
496Next Greater Element IEasyStack, Array, Hash Table +1
500Keyboard RowEasyArray, Hash Table, String
575Distribute CandiesEasyArray, Hash Table
594Longest Harmonious SubsequenceEasyArray, Hash Table, Counting +2
599Minimum Index Sum of Two ListsEasyArray, Hash Table, String
645Set MismatchEasyBit Manipulation, Array, Hash Table +1
653Two Sum IV - Input is a BSTEasyTree, Depth-First Search, Breadth-First Search +4
697Degree of an ArrayEasyArray, Hash Table
705Design HashSetEasyDesign, Array, Hash Table +2
706Design HashMapEasyDesign, Array, Hash Table +2
748Shortest Completing WordEasyArray, Hash Table, String
771Jewels and StonesEasyHash Table, String
804Unique Morse Code WordsEasyArray, Hash Table, String
819Most Common WordEasyArray, Hash Table, String +1
859Buddy StringsEasyHash Table, String
884Uncommon Words from Two SentencesEasyHash Table, String, Counting
888Fair Candy SwapEasyArray, Hash Table, Binary Search +1
914X of a Kind in a Deck of CardsEasyArray, Hash Table, Math +2
929Unique Email AddressesEasyArray, Hash Table, String
953Verifying an Alien DictionaryEasyArray, Hash Table, String
961N-Repeated Element in Size 2N ArrayEasyArray, Hash Table
997Find the Town JudgeEasyGraph, Array, Hash Table
1002Find Common CharactersEasyArray, Hash Table, String
1122Relative Sort ArrayEasyArray, Hash Table, Counting Sort +1
1128Number of Equivalent Domino PairsEasyArray, Hash Table, Counting
1160Find Words That Can Be Formed by CharactersEasyArray, Hash Table, String +1
1189Maximum Number of BalloonsEasyHash Table, String, Counting
1207Unique Number of OccurrencesEasyArray, Hash Table

Medium (115)

#ProblemDifficultyTopics
3Longest Substring Without Repeating CharactersMediumHash Table, String, Sliding Window
12Integer to RomanMediumHash Table, Math, String
17Letter Combinations of a Phone NumberMediumHash Table, String, Backtracking
36Valid SudokuMediumArray, Hash Table, Matrix
49Group AnagramsMediumArray, Hash Table, String +1
73Set Matrix ZeroesMediumArray, Hash Table, Matrix
105Construct Binary Tree from Preorder and Inorder TraversalMediumTree, Array, Hash Table +2
106Construct Binary Tree from Inorder and Postorder TraversalMediumTree, Array, Hash Table +2
128Longest Consecutive SequenceMediumUnion Find, Array, Hash Table
133Clone GraphMediumDepth-First Search, Breadth-First Search, Graph +1
138Copy List with Random PointerMediumHash Table, Linked List
139Word BreakMediumTrie, Memoization, Array +3
142Linked List Cycle IIMediumHash Table, Linked List, Two Pointers
146LRU CacheMediumDesign, Hash Table, Linked List +1
166Fraction to Recurring DecimalMediumHash Table, Math, String
187Repeated DNA SequencesMediumBit Manipulation, Hash Table, String +3
208Implement Trie (Prefix Tree)MediumDesign, Trie, Hash Table +1
229Majority Element IIMediumArray, Hash Table, Counting +1
264Ugly Number IIMediumHash Table, Math, Dynamic Programming +1
299Bulls and CowsMediumHash Table, String, Counting
347Top K Frequent ElementsMediumArray, Hash Table, Divide and Conquer +5
355Design TwitterMediumDesign, Hash Table, Linked List +1
380Insert Delete GetRandom O(1)MediumDesign, Array, Hash Table +2
395Longest Substring with At Least K Repeating CharactersMediumHash Table, String, Divide and Conquer +1
398Random Pick IndexMediumReservoir Sampling, Hash Table, Math +1
421Maximum XOR of Two Numbers in an ArrayMediumBit Manipulation, Trie, Array +1
423Reconstruct Original Digits from EnglishMediumHash Table, Math, String
424Longest Repeating Character ReplacementMediumHash Table, String, Sliding Window
433Minimum Genetic MutationMediumBreadth-First Search, Hash Table, String
438Find All Anagrams in a StringMediumHash Table, String, Sliding Window
442Find All Duplicates in an ArrayMediumArray, Hash Table, Sorting
447Number of BoomerangsMediumArray, Hash Table, Math
451Sort Characters By FrequencyMediumHash Table, String, Bucket Sort +3
4544Sum IIMediumArray, Hash Table
457Circular Array LoopMediumArray, Hash Table, Two Pointers
491Non-decreasing SubsequencesMediumBit Manipulation, Array, Hash Table +1
508Most Frequent Subtree SumMediumTree, Depth-First Search, Hash Table +1
519Random Flip MatrixMediumReservoir Sampling, Hash Table, Math +1
522Longest Uncommon Subsequence IIMediumArray, Hash Table, Two Pointers +2
523Continuous Subarray SumMediumArray, Hash Table, Math +1
525Contiguous ArrayMediumArray, Hash Table, Prefix Sum
532K-diff Pairs in an ArrayMediumArray, Hash Table, Two Pointers +2
535Encode and Decode TinyURLMediumDesign, Hash Table, String +1
554Brick WallMediumArray, Hash Table
560Subarray Sum Equals KMediumArray, Hash Table, Prefix Sum
567Permutation in StringMediumHash Table, Two Pointers, String +1
609Find Duplicate File in SystemMediumArray, Hash Table, String
621Task SchedulerMediumGreedy, Array, Hash Table +3
648Replace WordsMediumTrie, Array, Hash Table +1
652Find Duplicate SubtreesMediumTree, Depth-First Search, Hash Table +1
659Split Array into Consecutive SubsequencesMediumGreedy, Array, Hash Table +1
676Implement Magic DictionaryMediumDepth-First Search, Design, Trie +2
677Map Sum PairsMediumDesign, Trie, Hash Table +1
690Employee ImportanceMediumTree, Depth-First Search, Breadth-First Search +2
692Top K Frequent WordsMediumTrie, Array, Hash Table +5
720Longest Word in DictionaryMediumTrie, Array, Hash Table +2
721Accounts MergeMediumDepth-First Search, Breadth-First Search, Union Find +4
740Delete and EarnMediumArray, Hash Table, Dynamic Programming
752Open the LockMediumBreadth-First Search, Array, Hash Table +1
756Pyramid Transition MatrixMediumBit Manipulation, Hash Table, String +1
763Partition LabelsMediumGreedy, Hash Table, Two Pointers +1
767Reorganize StringMediumGreedy, Hash Table, String +3
781Rabbits in ForestMediumGreedy, Array, Hash Table +1
791Custom Sort StringMediumHash Table, String, Sorting
792Number of Matching SubsequencesMediumTrie, Array, Hash Table +4
811Subdomain Visit CountMediumArray, Hash Table, String +1
817Linked List ComponentsMediumArray, Hash Table, Linked List
820Short Encoding of WordsMediumTrie, Array, Hash Table +1
822Card Flipping GameMediumArray, Hash Table
823Binary Trees With FactorsMediumArray, Hash Table, Dynamic Programming +1
833Find And Replace in StringMediumArray, Hash Table, String +1
840Magic Squares In GridMediumArray, Hash Table, Math +1
846Hand of StraightsMediumGreedy, Array, Hash Table +1
863All Nodes Distance K in Binary TreeMediumTree, Depth-First Search, Breadth-First Search +2
865Smallest Subtree with all the Deepest NodesMediumTree, Depth-First Search, Breadth-First Search +2
869Reordered Power of 2MediumHash Table, Math, Counting +2
873Length of Longest Fibonacci SubsequenceMediumArray, Hash Table, Dynamic Programming
874Walking Robot SimulationMediumArray, Hash Table, Simulation
889Construct Binary Tree from Preorder and Postorder TraversalMediumTree, Array, Hash Table +2
890Find and Replace PatternMediumArray, Hash Table, String
893Groups of Special-Equivalent StringsMediumArray, Hash Table, String +1
904Fruit Into BasketsMediumArray, Hash Table, Sliding Window
911Online ElectionMediumDesign, Array, Hash Table +1
916Word SubsetsMediumArray, Hash Table, String
9233Sum With MultiplicityMediumArray, Hash Table, Two Pointers +2
930Binary Subarrays With SumMediumArray, Hash Table, Prefix Sum +1
939Minimum Area RectangleMediumGeometry, Array, Hash Table +2
947Most Stones Removed with Same Row or ColumnMediumDepth-First Search, Union Find, Graph +1
954Array of Doubled PairsMediumGreedy, Array, Hash Table +1
959Regions Cut By SlashesMediumDepth-First Search, Breadth-First Search, Union Find +3
963Minimum Area Rectangle IIMediumGeometry, Array, Hash Table +1
966Vowel SpellcheckerMediumArray, Hash Table, String
970Powerful IntegersMediumHash Table, Math, Enumeration
974Subarray Sums Divisible by KMediumArray, Hash Table, Prefix Sum
981Time Based Key-Value StoreMediumDesign, Hash Table, String +1
1010Pairs of Songs With Total Durations Divisible by 60MediumArray, Hash Table, Counting
1015Smallest Integer Divisible by KMediumHash Table, Math
1016Binary String With Substrings Representing 1 To NMediumBit Manipulation, Hash Table, String +1
1027Longest Arithmetic SubsequenceMediumArray, Hash Table, Binary Search +1
1048Longest String ChainMediumArray, Hash Table, Two Pointers +3
1054Distant BarcodesMediumGreedy, Array, Hash Table +3
1072Flip Columns For Maximum Number of Equal RowsMediumArray, Hash Table, Matrix
1079Letter Tile PossibilitiesMediumHash Table, String, Backtracking +1
1090Largest Values From LabelsMediumGreedy, Array, Hash Table +2
1110Delete Nodes And Return ForestMediumTree, Depth-First Search, Array +2
1123Lowest Common Ancestor of Deepest LeavesMediumTree, Depth-First Search, Breadth-First Search +2
1124Longest Well-Performing IntervalMediumStack, Array, Hash Table +2
1138Alphabet Board PathMediumHash Table, String
1146Snapshot ArrayMediumDesign, Array, Hash Table +1
1156Swap For Longest Repeated Character SubstringMediumHash Table, String, Sliding Window
1169Invalid TransactionsMediumArray, Hash Table, String +1
1170Compare Strings by Frequency of the Smallest CharacterMediumArray, Hash Table, String +2
1171Remove Zero Sum Consecutive Nodes from Linked ListMediumHash Table, Linked List
1177Can Make Palindrome from SubstringMediumBit Manipulation, Array, Hash Table +2
1202Smallest String With SwapsMediumDepth-First Search, Breadth-First Search, Union Find +4

Hard (38)

#ProblemDifficultyTopics
30Substring with Concatenation of All WordsHardHash Table, String, Sliding Window
37Sudoku SolverHardArray, Hash Table, Backtracking +1
41First Missing PositiveHardArray, Hash Table
76Minimum Window SubstringHardHash Table, String, Sliding Window
126Word Ladder IIHardBreadth-First Search, Hash Table, String +1
127Word LadderHardBreadth-First Search, Hash Table, String
140Word Break IIHardTrie, Memoization, Array +4
149Max Points on a LineHardGeometry, Array, Hash Table +1
336Palindrome PairsHardTrie, Array, Hash Table +1
352Data Stream as Disjoint IntervalsHardUnion Find, Design, Hash Table +3
381Insert Delete GetRandom O(1) - Duplicates allowedHardDesign, Array, Hash Table +2
391Perfect RectangleHardGeometry, Array, Hash Table +2
432All O`one Data StructureHardDesign, Hash Table, Linked List +1
460LFU CacheHardDesign, Hash Table, Linked List +1
480Sliding Window MedianHardArray, Hash Table, Sliding Window +1
632Smallest Range Covering Elements from K ListsHardGreedy, Array, Hash Table +3
691Stickers to Spell WordHardBit Manipulation, Memoization, Array +5
710Random Pick with BlacklistHardArray, Hash Table, Math +3
736Parse Lisp ExpressionHardStack, Recursion, Hash Table +1
745Prefix and Suffix SearchHardDesign, Trie, Array +2
815Bus RoutesHardBreadth-First Search, Array, Hash Table
828Count Unique Characters of All Substrings of a Given StringHardHash Table, String, Dynamic Programming
839Similar String GroupsHardDepth-First Search, Breadth-First Search, Union Find +3
854K-Similar StringsHardBreadth-First Search, Hash Table, String
895Maximum Frequency StackHardStack, Design, Hash Table +1
924Minimize Malware SpreadHardDepth-First Search, Breadth-First Search, Union Find +3
928Minimize Malware Spread IIHardDepth-First Search, Breadth-First Search, Union Find +3
952Largest Component Size by Common FactorHardUnion Find, Array, Hash Table +2
982Triples with Bitwise AND Equal To ZeroHardBit Manipulation, Array, Hash Table
987Vertical Order Traversal of a Binary TreeHardTree, Depth-First Search, Breadth-First Search +3
992Subarrays with K Different IntegersHardArray, Hash Table, Counting +1
996Number of Squareful ArraysHardBit Manipulation, Array, Hash Table +4
1001Grid IlluminationHardArray, Hash Table
1036Escape a Large MazeHardDepth-First Search, Breadth-First Search, Array +1
1074Number of Submatrices That Sum to TargetHardArray, Hash Table, Matrix +1
1096Brace Expansion IIHardStack, Breadth-First Search, Hash Table +3
1172Dinner Plate StacksHardStack, Design, Hash Table +1
1178Number of Valid Words for Each PuzzleHardBit Manipulation, Trie, Array +2

When the Hash Table 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.