The DSA roadmap
The roadmap covers 23 topics, in the order they build on each other. Each topic splits into patterns. Every pattern says how to spot it, teaches the idea in a lesson, then lists the problems that practise it.
01Foundationsstart here7 patterns · 18 lessons45
The language skills every later topic assumes: reading input, loops, functions, basic maths and how to measure how fast code runs.
Thinking like a programmerYou are new to code, or you freeze when a problem has no obvious first step.
- What a program is and what the computer does with itVideo soonSoon
- Breaking a problem into steps: flowcharts and pseudocodeVideo soonSoon
- Dry runs, edge cases and debugging by handVideo soonSoon
- Choosing a language for interviewsVideo soonSoon
Language basicsYou need to read input, branch on conditions and repeat work in your language.4
- Input, output and data typesVideo soonSoon
- User input and output (opens GFG)GFGGFGEasy
- Data types (opens GFG)GFGGFGEasy
- If-else statements (opens GFG)GFGGFGEasy
- Switch statements (opens GFG)GFGGFGEasy
- Loops and functionsVideo soonSoon
- Your standard library: C++ STL, Java Collections, Python built-insVideo soonSoon
Time and space complexityYou need to say how the running time grows before you write any code.
- Big-O: counting how work grows with inputVideo soonSoon
- Reading complexity from code: loops, recursion and hidden costsVideo soonSoon
- Using the constraints to guess the intended complexityVideo soonSoon
Loops through pattern printingYou want nested loops to feel automatic before you meet real problems.22
- How to read a pattern: rows, columns and what changesVideo soonSoon
- Square of starsSolveEasy
- Right triangle of starsSolveEasy
- Right triangle of numbersSolveEasy
- Triangle of repeated numbersSolveEasy
- Inverted right triangle of starsSolveEasy
- Inverted right triangle of numbersSolveEasy
- Star pyramidSolveEasy
- Inverted star pyramidSolveEasy
- Star diamondSolveEasy
- Half diamondSolveEasy
- Triangle of alternating 1s and 0sSolveEasy
- Number crownSolveEasy
- Increasing number triangleSolveEasy
- Increasing letter triangleSolveEasy
- Reverse letter triangleSolveEasy
- Letter rampSolveEasy
- Letter pyramidSolveEasy
- Letter triangle from the endSolveEasy
- Symmetric voidSolveMedium
- Symmetric butterflySolveMedium
- Hollow squareSolveMedium
- Concentric number squaresSolveMedium
Working with digits and divisorsThe input is a number and you need to take it apart digit by digit or find what divides it.10
- Extracting digits with % and /Video soonSoon
- Count the digits of a numberSolveEasy
- Count the odd digits of a numberSolveEasy
- Largest digit of a numberSolveEasy
- Palindrome NumberSolveEasy
- Armstrong NumberSolveEasy
- Perfect NumberSolveEasy
- Divisors and primes in O(√n)Video soonSoon
- All divisors of a numberSolveMedium
- Check whether a number is primeSolveMedium
- GCD with the Euclidean algorithmVideo soonSoon
- GCD of two numbersSolveMedium
- LCM of two numbersSolveMedium
Recursion basicsA function needs to solve a smaller copy of the same problem.5
- How recursion works: base case and the call stackVideo soonSoon
- Print something N times with recursion (opens GFG)GFGGFGEasy
- Passing the answer down or returning it upVideo soonSoon
- Sum of the first N numbersSolveEasy
- Factorial of NSolveEasy
- Sum of the digits of a numberSolveEasy
- Fibonacci Number147 DSA SheetSolveEasy
- Drawing the recursion tree to count callsVideo soonSoon
Counting with arrays and mapsYou need how often each value appears, and an array index or a map key can hold the count.4
- Hashing: counting with an array, then with a mapVideo soonSoon
- Count the frequency of each elementSolveEasy
- Most frequent elementSolveMedium
- Second most frequent elementSolveMedium
- Sum of the highest and lowest frequencySolveMedium
02Arrays and Hashingafter Foundations7 patterns · 3 lessons43
Most problems start with an array and a hash map. This topic teaches the moves that every later topic reuses.
Array basicsYou need to scan an array once to find, count or move elements.12
- Arrays in memory: indexing, copying and resizingVideo soonSoon
- Linear searchSolveEasy
- Largest elementSolveEasy
- Second largest elementSolveEasy
- Sum of array elementsSolveEasy
- Count the odd numbers in an arraySolveEasy
- Reverse an arraySolveEasy
- Check whether an array is sortedSolveEasy
- Max Consecutive OnesSolveEasy
- Find All Numbers Disappeared in an ArraySolveEasy
- Left rotate an array by oneSolveEasy
- Rotate ArraySolveMedium
- Leaders in an arraySolveMedium
Hash map lookupsYou need to know in one step whether you have seen a value before.9
- Two Sum147 DSA SheetSolveEasy
- Contains Duplicate147 DSA SheetSolveEasy
- Valid Anagram147 DSA SheetSolveEasy
- Group Anagrams147 DSA SheetSolveMedium
- Longest Consecutive Sequence147 DSA SheetSolveMedium
- Valid SudokuSolveMedium
- Encode and Decode StringsSolveMedium
- First Unique Character in a StringSolveEasy
- First Missing PositiveSolveHard
Counting and votingThe answer depends on how often each value appears.4
- Top K Frequent Elements147 DSA SheetSolveMedium
- Majority Element147 DSA SheetSolveEasy
- Majority Element IISolveMedium
- Detect SquaresSolveMedium
One pass with running stateYou keep one or two values as you scan, and the answer is ready at the end.3
- Best Time to Buy and Sell Stock147 DSA SheetSolveEasy
- Maximum Subarray147 DSA SheetSolveMedium
- Maximum Product Subarray147 DSA SheetSolveMedium
Prefix sumsThe question is about the sum of a subarray, and adding it up again for every range is too slow.10
- Prefix sums and the running-sum mapVideo soonSoon
- Product of Array Except Self147 DSA SheetSolveMedium
- Subarray Sum Equals K147 DSA SheetSolveMedium
- Maximum Size Subarray Sum Equals kSolveMedium
- Range Sum Query - ImmutableSolveEasy
- Range Sum Query 2D - ImmutableSolveMedium
- Contiguous ArraySolveMedium
- Subarray Sums Divisible by KSolveMedium
- Continuous Subarray SumSolveMedium
- Longest subarray with sum 0SolveMedium
- Count subarrays with given xor KSolveMedium
Walking a matrixThe input is a grid, and the work is moving through it in a fixed order.3
- Spiral Matrix147 DSA SheetSolveMedium
- Rotate ImageSolveMedium
- Set Matrix ZeroesSolveMedium
Counting during merge sortYou must count pairs where an earlier value is larger than a later one.2
- Counting pairs while merge sortingVideo soonSoon
- Reverse PairsSolveHard
- Count InversionsSolveMedium
03Mathafter Foundations4 patterns · 3 lessons14
A few problems test arithmetic more than data structures. Each one has a single idea to learn.
Number tricksThe input is a number, or digits stored in a string or an array.8
- Plus One147 DSA SheetSolveEasy
- Happy NumberSolveEasy
- Pow(x, n)147 DSA SheetSolveMedium
- Reverse IntegerSolveMedium
- Multiply StringsSolveMedium
- Count PrimesSolveMedium
- Ugly NumberSolveEasy
- Nim GameSolveEasy
Primes and the sieveYou need primes or prime factors for many numbers at once.2
- The sieve of EratosthenesVideo soonSoon
- Count primes in a rangeSolveHard
- Prime factorisationSolveMedium
Fast power and modular arithmeticThe answer is huge, so you work modulo a prime and square as you go.1
- Fast exponentiation and modular arithmeticVideo soonSoon
- Count Good NumbersSolveMedium
Pascal's triangle and combinationsYou need nCr, or the rows of Pascal's triangle.3
- Computing nCr without overflowVideo soonSoon
- Pascal's TriangleSolveEasy
- Pascal's Triangle IISolveEasy
- Pascal's triangle: a single valueSolveMedium
04Bit Manipulationafter Arrays and Hashing4 patterns · 2 lessons20
Integers are rows of bits. A few operators, mostly XOR and shifts, solve problems that seem to need extra memory.
Bit basicsYou need to read, set or clear one bit of a number.7
- Binary, two's complement and the bit operatorsVideo soonSoon
- Is the i-th bit setSolveEasy
- Is a number odd, with bitsSolveEasy
- Power of TwoSolveEasy
- Set the rightmost unset bitSolveEasy
- Swap two numbers without a third variableSolveEasy
- Hamming DistanceSolveEasy
- Minimum Bit Flips to Convert NumberSolveEasy
XOR tricksEvery value appears twice except one or two, or one value is missing.6
- Single Number147 DSA SheetSolveEasy
- Missing Number147 DSA SheetSolveEasy
- Single Number IISolveMedium
- Single Number IIISolveMedium
- Set MismatchSolveEasy
- XOR of all numbers in a rangeSolveMedium
Counting and shifting bitsThe question is about the bits of a number.6
- Number of 1 Bits147 DSA SheetSolveEasy
- Counting Bits147 DSA SheetSolveEasy
- Reverse Bits147 DSA SheetSolveEasy
- Sum of Two IntegersSolveMedium
- Divide Two IntegersSolveMedium
- Bitwise AND of Numbers RangeSolveMedium
BitmasksThe input is small enough that every subset fits in an integer.1
- Bitmasks: every subset as a numberVideo soonSoon
- All subsets with a bitmaskSolveMedium
05Stringsafter Arrays and Hashing4 patterns · 1 lesson20
Strings are arrays of characters with a few traps: they are often immutable, and building one in a loop can be quadratic.
String basicsYou need to reverse, compare or rebuild a string character by character.10
- Strings in memory: immutability and building strings efficientlyVideo soonSoon
- Reverse StringSolveEasy
- Valid Palindrome IISolveEasy
- Largest Odd Number in StringSolveEasy
- Longest Common Prefix147 DSA SheetSolveEasy
- Isomorphic StringsSolveEasy
- Rotate StringSolveEasy
- Longest PalindromeSolveEasy
- Reverse Words in a StringSolveMedium
- String CompressionSolveMedium
- Count and SaySolveMedium
Parentheses and nestingThe string is made of brackets and you track how deep you are.4
Parsing and conversionYou turn text into a number or a number into text, with every edge case handled.3
Counting over substringsYou need a statistic for every substring, and there are O(n²) of them.3
06Sortingafter Arrays and Hashing2 patterns · 3 lessons9
Knowing how the sorts work tells you what a sort costs, when order is kept, and when a problem is really a sorting problem.
Simple sortsYou need to understand O(n²) sorting well enough to write it without thinking.5
- What sorting costs, and what "stable" meansVideo soonSoon
- Selection sortSolveMedium
- Bubble sortSolveMedium
- Insertion sortSolveMedium
- Bubble sort with recursionSolveMedium
- Insertion sort with recursionSolveMedium
Divide-and-conquer sortsYou need O(n log n), and splitting the array in half is the way in.4
- Merge sort and quick sortVideo soonSoon
- Merge sortSolveMedium
- Quick sortSolveMedium
- Sort an Array147 DSA SheetSolveMedium
- Counting sort and bucket sort: sorting without comparisonsVideo soonSoon
- Largest NumberSolveMedium
07Two Pointersafter Arrays and Hashing3 patterns · 1 lesson18
Two indexes move through the same array. One pass then does the work of two nested loops.
Pointers from both endsThe array is sorted, or can be, and you are looking for a pair.9
- Two pointers: why one pass is enoughVideo soonSoon
- Valid Palindrome147 DSA SheetSolveEasy
- Two Sum II - Input Array Is Sorted147 DSA SheetSolveMedium
- 3Sum147 DSA SheetSolveMedium
- Container With Most Water147 DSA SheetSolveMedium
- 4SumSolveMedium
- Trapping Rain Water147 DSA SheetSolveHard
- Squares of a Sorted ArraySolveEasy
- 3Sum ClosestSolveMedium
- Boats to Save PeopleSolveMedium
Rearranging in placeYou must reorder the array without making a second one.5
- Sort Colors147 DSA SheetSolveMedium
- Next Permutation147 DSA SheetSolveMedium
- Move ZeroesSolveEasy
- Remove Duplicates from Sorted ArraySolveEasy
- Rearrange Array Elements by SignSolveMedium
Merging sorted arraysYou have two sorted sequences and need their union, intersection or merge.4
08Sliding Windowafter Two Pointers3 patterns · 2 lessons16
A window over a range grows on one side and shrinks on the other. Each element enters once and leaves once.
Longest or shortest windowThe question asks for the longest or shortest substring or subarray that meets a condition.8
- The sliding window template: grow, then shrinkVideo soonSoon
- Longest Substring Without Repeating Characters147 DSA SheetSolveMedium
- Max Consecutive Ones III147 DSA SheetSolveMedium
- Longest Repeating Character Replacement147 DSA SheetSolveMedium
- Longest Substring with At Most K Distinct Characters (opens LeetCode)LeetCodeLCMedium
- Minimum Window Substring147 DSA SheetSolveHard
- Minimum Size Subarray Sum (opens LeetCode)LeetCodeLCMedium
- Fruit Into Baskets (opens LeetCode)LeetCodeLCMedium
- Minimum Window Subsequence (opens LeetCode)LeetCodeLCHard
Fixed-size windowThe window size is given, so each step adds one element and removes one.4
- Permutation in String147 DSA SheetSolveMedium
- Maximum Points You Can Obtain from Cards147 DSA SheetSolveMedium
- Maximum Average Subarray I (opens LeetCode)LeetCodeLCEasy
- Find All Anagrams in a String (opens LeetCode)LeetCodeLCMedium
Counting windowsThe question asks how many subarrays meet a condition, not which one is longest.4
- Counting with "at most K" minus "at most K−1"Video soonSoon
- Binary Subarrays With Sum147 DSA SheetSolveMedium
- Count Number of Nice Subarrays (opens LeetCode)LeetCodeLCMedium
- Number of Substrings Containing All Three Characters (opens LeetCode)LeetCodeLCMedium
- Subarrays with K Different Integers (opens LeetCode)LeetCodeLCHard
09Stacks and Queuesafter Arrays and Hashing5 patterns · 3 lessons39
A stack remembers what is still open. The last thing you pushed is the first thing you need.
Building stacks and queuesYou need to implement a stack or queue yourself, or build one from the other.8
- Stacks and queues: operations, uses and costsVideo soonSoon
- Implement a stack with an array (opens GFG)GFGGFGMedium
- Implement a queue with an array (opens GFG)GFGGFGMedium
- Implement a stack with a linked list (opens GFG)GFGGFGMedium
- Implement a queue with a linked list (opens GFG)GFGGFGMedium
- Implement Stack using Queues (opens LeetCode)LeetCodeLCEasy
- Implement Queue using Stacks (opens LeetCode)LeetCodeLCEasy
- Design Circular Queue (opens LeetCode)LeetCodeLCMedium
- Design Circular Deque (opens LeetCode)LeetCodeLCMedium
Matching and cancellingEach new item either cancels the most recent open item or waits on top of it.10
- Valid Parentheses147 DSA SheetSolveEasy
- Min Stack147 DSA SheetSolveMedium
- Evaluate Reverse Polish Notation147 DSA SheetSolveMedium
- Asteroid Collision (opens LeetCode)LeetCodeLCMedium
- Find the Celebrity (opens LeetCode)LeetCodeLCMedium
- Baseball Game (opens LeetCode)LeetCodeLCEasy
- Backspace String Compare (opens LeetCode)LeetCodeLCEasy
- Simplify Path (opens LeetCode)LeetCodeLCMedium
- Decode String (opens LeetCode)LeetCodeLCMedium
- Basic Calculator II (opens LeetCode)LeetCodeLCMedium
Infix, prefix and postfixYou need to convert or evaluate an expression written in another notation.6
- Infix, prefix and postfix, and why postfix needs no bracketsVideo soonSoon
- Infix to postfix (opens GFG)GFGGFGMedium
- Infix to prefix (opens GFG)GFGGFGMedium
- Postfix to infix (opens GFG)GFGGFGMedium
- Prefix to infix (opens GFG)GFGGFGMedium
- Prefix to postfix (opens GFG)GFGGFGMedium
- Postfix to prefix (opens GFG)GFGGFGMedium
Monotonic stackFor each element you need the next or previous element that is larger or smaller.13
- The monotonic stack templateVideo soonSoon
- Next Greater Element I147 DSA SheetSolveEasy
- Daily Temperatures147 DSA SheetSolveMedium
- Next Greater Element II (opens LeetCode)LeetCodeLCMedium
- Online Stock Span (opens LeetCode)LeetCodeLCMedium
- Car Fleet147 DSA SheetSolveMedium
- Remove K Digits (opens LeetCode)LeetCodeLCMedium
- Sum of Subarray Minimums (opens LeetCode)LeetCodeLCMedium
- Sum of Subarray Ranges (opens LeetCode)LeetCodeLCMedium
- Largest Rectangle in Histogram147 DSA SheetSolveHard
- Maximal Rectangle (opens LeetCode)LeetCodeLCHard
- Next smaller element (opens GFG)GFGGFGMedium
- Number of greater elements to the right (opens GFG)GFGGFGMedium
- 132 Pattern (opens LeetCode)LeetCodeLCMedium
Monotonic queueYou need the largest or smallest value of every window as it slides.2
- Sliding Window Maximum147 DSA SheetSolveHard
- Sliding Window Median (opens LeetCode)LeetCodeLCHard
10Binary Searchafter Arrays and Hashing5 patterns · 2 lessons36
Each step throws away half of what is left. It works on sorted arrays, and on any answer you can check with a yes or no.
Lower and upper boundsYou need the first position where a condition becomes true.8
- Binary search without off-by-one errors: the invariantVideo soonSoon
- Lower bound (opens GFG)GFGGFGMedium
- Upper bound (opens GFG)GFGGFGMedium
- Search Insert Position (opens LeetCode)LeetCodeLCEasy
- Floor and ceil in a sorted arrayComing soonSoonMedium
- Find First and Last Position of Element in Sorted Array (opens LeetCode)LeetCodeLCMedium
- Count occurrences in a sorted array (opens GFG)GFGGFGMedium
- How many times is a sorted array rotated (opens GFG)GFGGFGMedium
- Peak Index in a Mountain Array (opens LeetCode)LeetCodeLCMedium
Searching a sorted arrayThe input is sorted, or sorted and then rotated.7
- Binary Search147 DSA SheetSolveEasy
- Search in Rotated Sorted Array147 DSA SheetSolveMedium
- Find Minimum in Rotated Sorted Array147 DSA SheetSolveMedium
- Search in Rotated Sorted Array II (opens LeetCode)LeetCodeLCMedium
- Single Element in a Sorted Array (opens LeetCode)LeetCodeLCMedium
- Find Peak Element147 DSA SheetSolveMedium
- Time Based Key-Value Store (opens LeetCode)LeetCodeLCMedium
Searching the answerYou cannot compute the answer directly, but you can quickly check whether a given value works.13
- Binary search on the answer: the monotonic checkVideo soonSoon
- Sqrt(x)147 DSA SheetSolveEasy
- Koko Eating Bananas147 DSA SheetSolveMedium
- Magnetic Force Between Two Balls147 DSA SheetSolveMedium
- Split Array Largest Sum147 DSA SheetSolveHard
- Nth root of a number (opens GFG)GFGGFGMedium
- Find the Smallest Divisor Given a Threshold (opens LeetCode)LeetCodeLCMedium
- Minimum Number of Days to Make m Bouquets (opens LeetCode)LeetCodeLCMedium
- Capacity To Ship Packages Within D Days (opens LeetCode)LeetCodeLCMedium
- Kth Missing Positive Number (opens LeetCode)LeetCodeLCEasy
- Aggressive cows (opens GFG)GFGGFGHard
- Book allocation (opens GFG)GFGGFGHard
- Painter's partition (opens GFG)GFGGFGHard
- Minimize Max Distance to Gas Station (opens LeetCode)LeetCodeLCHard
Searching a matrixThe input is a sorted matrix, and you search rows, columns or values.6
- Search a 2D Matrix (opens LeetCode)LeetCodeLCMedium
- Search a 2D Matrix II (opens LeetCode)LeetCodeLCMedium
- Kth Smallest Element in a Sorted Matrix (opens LeetCode)LeetCodeLCMedium
- Row with the most 1s (opens GFG)GFGGFGMedium
- Find a Peak Element II (opens LeetCode)LeetCodeLCMedium
- Median of a Row Wise Sorted Matrix (opens LeetCode)LeetCodeLCMedium
Partition searchYou need the median or the k-th element of two sorted arrays.2
- Median of Two Sorted Arrays147 DSA SheetSolveHard
- K-th element of two sorted arrays (opens GFG)GFGGFGHard
11Linked Listsafter Two Pointers5 patterns · 4 lessons52
Linked list problems are about moving pointers without losing the rest of the list.
Singly linked list basicsYou need to walk, insert into or delete from a linked list by hand.14
- Linked lists: nodes, pointers and the headVideo soonSoon
- Traverse a linked list (opens GFG)GFGGFGEasy
- Length of a linked list (opens GFG)GFGGFGEasy
- Search in a linked list (opens GFG)GFGGFGEasy
- Insert at the head (opens GFG)GFGGFGMedium
- Insert at the tail (opens GFG)GFGGFGMedium
- Insert at position KComing soonSoonMedium
- Insert before the value XComing soonSoonMedium
- Delete the head (opens GFG)GFGGFGMedium
- Delete the tail (opens GFG)GFGGFGMedium
- Delete the K-th nodeComing soonSoonMedium
- Delete the node with value XComing soonSoonMedium
- Delete Node in a Linked List (opens LeetCode)LeetCodeLCMedium
- Remove Linked List Elements (opens LeetCode)LeetCodeLCEasy
- Design Linked List (opens LeetCode)LeetCodeLCMedium
Doubly linked listsEach node also points back, so you must keep two links right on every change.13
- Doubly linked lists: keeping prev and next in stepVideo soonSoon
- Build a doubly linked list from an array (opens GFG)GFGGFGMedium
- Insert before the head (opens GFG)GFGGFGMedium
- Insert before the tailComing soonSoonMedium
- Insert before the K-th nodeComing soonSoonMedium
- Insert before a given nodeComing soonSoonMedium
- Delete the head (opens GFG)GFGGFGMedium
- Delete the tailComing soonSoonMedium
- Delete the K-th nodeComing soonSoonMedium
- Delete a given nodeComing soonSoonMedium
- Reverse a doubly linked list (opens GFG)GFGGFGMedium
- Delete every occurrence of a key (opens GFG)GFGGFGMedium
- Remove duplicates from a sorted doubly linked list (opens GFG)GFGGFGMedium
- Pairs with a given sum in a doubly linked list (opens GFG)GFGGFGMedium
Fast and slow pointersYou need the middle, a cycle, or a node counted from the end.9
- Fast and slow pointers: why they meetVideo soonSoon
- Middle of the Linked List147 DSA SheetSolveEasy
- Linked List Cycle147 DSA SheetSolveEasy
- Remove Nth Node From End of List147 DSA SheetSolveMedium
- Palindrome Linked List (opens LeetCode)LeetCodeLCEasy
- Intersection of Two Linked Lists (opens LeetCode)LeetCodeLCEasy
- Linked List Cycle II147 DSA SheetSolveMedium
- Find the Duplicate Number (opens LeetCode)LeetCodeLCMedium
- Delete the Middle Node of a Linked List (opens LeetCode)LeetCodeLCMedium
- Length of the loop in a linked list (opens GFG)GFGGFGEasy
Reversing and rewiringThe answer is the same nodes, connected in a different order.14
- The dummy node trickVideo soonSoon
- Reverse Linked List147 DSA SheetSolveEasy
- Merge Two Sorted Lists147 DSA SheetSolveEasy
- Reorder List147 DSA SheetSolveMedium
- Add Two Numbers147 DSA SheetSolveMedium
- Rotate List (opens LeetCode)LeetCodeLCMedium
- Sort List (opens LeetCode)LeetCodeLCMedium
- Copy List with Random Pointer147 DSA SheetSolveMedium
- Reverse Nodes in k-Group147 DSA SheetSolveHard
- Odd Even Linked List (opens LeetCode)LeetCodeLCMedium
- Reverse Linked List II (opens LeetCode)LeetCodeLCMedium
- Plus One Linked List (opens LeetCode)LeetCodeLCMedium
- Next Greater Node In Linked List (opens LeetCode)LeetCodeLCMedium
- Sort a linked list of 0s, 1s and 2s (opens GFG)GFGGFGMedium
- Flatten a linked list (opens GFG)GFGGFGHard
A list and a map togetherYou must find any item in one step and also keep the items in order of use.2
- LRU Cache147 DSA SheetSolveMedium
- LFU Cache (opens LeetCode)LeetCodeLCHard
12Recursion and Backtrackingafter Arrays and Hashing4 patterns · 3 lessons25
Recursion tries one choice and explores everything after it. Then it undoes the choice and tries the next one.
Pick or skipEach element is either in or out, and you need every choice or a count of them.3
- The pick-or-skip tree: generating subsequencesVideo soonSoon
- Does a subsequence with sum K existComing soonSoonMedium
- Count subsequences with sum K (opens GFG)GFGGFGMedium
- Binary strings without consecutive 1s (opens GFG)GFGGFGMedium
Recursion on a stackYou may use only the stack's own push and pop, so the call stack holds the rest.2
Generating every choiceThe question asks for all subsets, combinations or permutations.12
- Backtracking: choose, explore, undoVideo soonSoon
- Subsets147 DSA SheetSolveMedium
- Permutations147 DSA SheetSolveMedium
- Subsets II147 DSA SheetSolveMedium
- Combination Sum147 DSA SheetSolveMedium
- Combination Sum II147 DSA SheetSolveMedium
- Letter Combinations of a Phone Number147 DSA SheetSolveMedium
- Generate Parentheses147 DSA SheetSolveMedium
- Palindrome Partitioning147 DSA SheetSolveMedium
- Combinations (opens LeetCode)LeetCodeLCMedium
- Permutations II (opens LeetCode)LeetCodeLCMedium
- Combination Sum III (opens LeetCode)LeetCodeLCMedium
- Gray Code (opens LeetCode)LeetCodeLCMedium
Searching with constraintsMost choices break a rule, so you check each one early and stop exploring it.8
- Pruning: stopping a branch earlyVideo soonSoon
- Word Search147 DSA SheetSolveMedium
- N-Queens147 DSA SheetSolveHard
- Sudoku Solver (opens LeetCode)LeetCodeLCHard
- Rat in a maze (opens GFG)GFGGFGHard
- M-coloring (opens GFG)GFGGFGHard
- Beautiful Arrangement (opens LeetCode)LeetCodeLCMedium
- Word Break II (opens LeetCode)LeetCodeLCHard
- Expression Add Operators (opens LeetCode)LeetCodeLCHard
13Binary Treesafter Recursion and Backtracking4 patterns · 7 lessons44
Almost every tree problem is one recursive function. The skill is deciding what each call returns to its parent.
TraversalsYou need to visit every node in a set order: pre, in, post or level.6
- Trees: nodes, depth, height and how they are storedVideo soonSoon
- Preorder, inorder and postorderVideo soonSoon
- Binary Tree Preorder Traversal (opens LeetCode)LeetCodeLCEasy
- Binary Tree Inorder Traversal (opens LeetCode)LeetCodeLCEasy
- Binary Tree Postorder Traversal (opens LeetCode)LeetCodeLCEasy
- Iterative traversals with an explicit stackVideo soonSoon
- Preorder, inorder and postorder in one pass (opens GFG)GFGGFGMedium
- Morris traversal: O(1) space by threading the treeVideo soonSoon
- Morris inorder traversal (opens GFG)GFGGFGHard
- Morris preorder traversal (opens GFG)GFGGFGHard
Depth first: return a value upThe answer for a node depends on the answers for its children.23
- What should a recursive call return?Video soonSoon
- Maximum Depth of Binary Tree147 DSA SheetSolveEasy
- Invert Binary Tree147 DSA SheetSolveEasy
- Same Tree147 DSA SheetSolveEasy
- Subtree of Another Tree147 DSA SheetSolveEasy
- Balanced Binary Tree (opens LeetCode)LeetCodeLCEasy
- Diameter of Binary Tree147 DSA SheetSolveEasy
- Count Good Nodes in Binary Tree (opens LeetCode)LeetCodeLCMedium
- Binary Tree Paths (opens LeetCode)LeetCodeLCEasy
- Lowest Common Ancestor of a Binary Tree147 DSA SheetSolveMedium
- Binary Tree Maximum Path Sum147 DSA SheetSolveHard
- Boundary of Binary Tree (opens LeetCode)LeetCodeLCMedium
- Minimum Depth of Binary Tree (opens LeetCode)LeetCodeLCEasy
- Symmetric Tree (opens LeetCode)LeetCodeLCEasy
- Merge Two Binary Trees (opens LeetCode)LeetCodeLCEasy
- Path Sum (opens LeetCode)LeetCodeLCEasy
- Path Sum II (opens LeetCode)LeetCodeLCMedium
- Sum Root to Leaf Numbers (opens LeetCode)LeetCodeLCMedium
- Path Sum III (opens LeetCode)LeetCodeLCMedium
- Children sum property (opens GFG)GFGGFGMedium
- Path from the root to a node (opens GFG)GFGGFGMedium
- Count Complete Tree Nodes (opens LeetCode)LeetCodeLCMedium
- Flatten Binary Tree to Linked List (opens LeetCode)LeetCodeLCMedium
- House Robber III (opens LeetCode)LeetCodeLCMedium
Breadth first: level by levelThe question mentions levels, a view from one side, or a distance from a node.12
- Level order with a queueVideo soonSoon
- Binary Tree Level Order Traversal147 DSA SheetSolveMedium
- Binary Tree Right Side View147 DSA SheetSolveMedium
- Maximum Width of Binary Tree (opens LeetCode)LeetCodeLCMedium
- All Nodes Distance K in Binary Tree147 DSA SheetSolveMedium
- Amount of Time for Binary Tree to Be Infected (opens LeetCode)LeetCodeLCMedium
- Vertical Order Traversal of a Binary Tree (opens LeetCode)LeetCodeLCHard
- Binary Tree Zigzag Level Order Traversal (opens LeetCode)LeetCodeLCMedium
- Binary Tree Level Order Traversal II (opens LeetCode)LeetCodeLCMedium
- Average of Levels in Binary Tree (opens LeetCode)LeetCodeLCEasy
- Populating Next Right Pointers in Each Node (opens LeetCode)LeetCodeLCMedium
- Top view of a binary tree (opens GFG)GFGGFGMedium
- Bottom view of a binary tree (opens GFG)GFGGFGMedium
Building and serializingYou must turn traversal orders, or a string, back into a tree.3
- Which traversal pairs define a unique treeVideo soonSoon
- Construct Binary Tree from Preorder and Inorder Traversal147 DSA SheetSolveMedium
- Serialize and Deserialize Binary Tree147 DSA SheetSolveHard
- Construct Binary Tree from Inorder and Postorder Traversal (opens LeetCode)LeetCodeLCMedium
14Binary Search Treesafter Binary Trees, Binary Search2 patterns · 1 lesson18
In a BST, everything on the left is smaller and everything on the right is larger. Most problems use this to skip half the tree.
BST basicsYou need to search, find the smallest or largest, or find the nearest value in a BST.5
- What makes a tree a BSTVideo soonSoon
- Search in a Binary Search Tree (opens LeetCode)LeetCodeLCEasy
- Minimum and maximum in a BST (opens GFG)GFGGFGEasy
- Floor and ceil in a BST (opens GFG)GFGGFGMedium
- Range Sum of BST (opens LeetCode)LeetCodeLCEasy
- Convert Sorted Array to Binary Search Tree (opens LeetCode)LeetCodeLCEasy
Using the orderingThe tree is a BST, so an in-order walk visits the values in sorted order.13
- Validate Binary Search Tree147 DSA SheetSolveMedium
- Kth Smallest Element in a BST147 DSA SheetSolveMedium
- Lowest Common Ancestor of a Binary Search Tree147 DSA SheetSolveMedium
- Insert into a Binary Search Tree (opens LeetCode)LeetCodeLCMedium
- Delete Node in a BST147 DSA SheetSolveMedium
- Construct Binary Search Tree from Preorder Traversal (opens LeetCode)LeetCodeLCMedium
- Two Sum IV - Input is a BST (opens LeetCode)LeetCodeLCEasy
- Recover Binary Search Tree147 DSA SheetSolveMedium
- Largest BST Subtree (opens LeetCode)LeetCodeLCMedium
- Trim a Binary Search Tree (opens LeetCode)LeetCodeLCMedium
- Inorder Successor in BST (opens LeetCode)LeetCodeLCMedium
- Binary Search Tree Iterator (opens LeetCode)LeetCodeLCMedium
- Maximum Sum BST in Binary Tree (opens LeetCode)LeetCodeLCHard
15Heapsafter Binary Trees5 patterns · 2 lessons22
A heap gives you the smallest or largest item in log time. Use it when you keep needing the current best.
Building a heapYou need to implement a heap yourself, or use one to sort.6
- Binary heaps: sift up, sift down and heapifyVideo soonSoon
- Why building a heap is O(n)Video soonSoon
- Implement Min Heap (opens GFG)GFGGFGMedium
- Implement a max heap (opens GFG)GFGGFGMedium
- Build a heap from an array (opens GFG)GFGGFGEasy
- Does an array represent a min heap (opens GFG)GFGGFGMedium
- Convert a min heap to a max heap (opens GFG)GFGGFGMedium
- Heap sort (opens GFG)GFGGFGHard
Top KYou need the k largest, smallest or closest items, and a full sort does more work than that.8
- Kth Largest Element in a Stream147 DSA SheetSolveEasy
- Kth Largest Element in an Array147 DSA SheetSolveMedium
- K Closest Points to Origin147 DSA SheetSolveMedium
- K-th smallest element with a heap (opens GFG)GFGGFGMedium
- Find K Closest Elements (opens LeetCode)LeetCodeLCMedium
- Sort a nearly sorted array (opens GFG)GFGGFGMedium
- Rank Transform of an Array (opens LeetCode)LeetCodeLCEasy
- Maximum sum combinations (opens GFG)GFGGFGMedium
Simulating with a heapEach step takes the best item, changes it, and puts it back.6
- Last Stone Weight147 DSA SheetSolveEasy
- Task Scheduler147 DSA SheetSolveMedium
- Design Twitter (opens LeetCode)LeetCodeLCMedium
- Connect ropes at minimum cost (opens GFG)GFGGFGEasy
- Reorganize String (opens LeetCode)LeetCodeLCMedium
- IPO (opens LeetCode)LeetCodeLCHard
Two heapsYou need the median of numbers that keep arriving.1
- Find Median from Data Stream147 DSA SheetSolveHard
Merging k sorted listsYou have k sorted sequences and need one sorted output.1
- Merge k Sorted Lists147 DSA SheetSolveHard
16Triesafter Binary Trees2 patterns · 2 lessons9
A trie stores strings by their prefixes, so words that start the same way share a path.
Prefix treesYou search many words by prefix, or match them one letter at a time.7
- Tries: one node per prefixVideo soonSoon
- Implement Trie (Prefix Tree)147 DSA SheetSolveMedium
- Design Add and Search Words Data Structure147 DSA SheetSolveMedium
- Longest Word in Dictionary (opens LeetCode)LeetCodeLCMedium
- Implement Trie II (Prefix Tree) (opens LeetCode)LeetCodeLCMedium
- Word Search II147 DSA SheetSolveHard
- Number of Distinct Substrings in a String (opens LeetCode)LeetCodeLCMedium
- Longest Word With All Prefixes (opens LeetCode)LeetCodeLCMedium
Tries over bitsYou want the pair of numbers with the largest XOR.2
- Storing numbers bit by bit in a trieVideo soonSoon
- Maximum XOR of Two Numbers in an Array (opens LeetCode)LeetCodeLCMedium
- Maximum XOR With an Element From Array (opens LeetCode)LeetCodeLCHard
17Graphsafter Binary Trees7 patterns · 6 lessons40
A graph is a tree that can have cycles. The same DFS and BFS apply, with a visited set so you do not loop forever.
Graph basicsYou need to store a graph and visit every node.3
- Graphs: vertices, edges, directed and weightedVideo soonSoon
- Adjacency list or adjacency matrixVideo soonSoon
- BFS and DFS on a graphVideo soonSoon
- BFS and DFS of a graph (opens GFG)GFGGFGEasy
- Keys and Rooms (opens LeetCode)LeetCodeLCMedium
- All Paths From Source to Target (opens LeetCode)LeetCodeLCMedium
Grids as graphsThe input is a grid, and each cell connects to its neighbours.8
- Flood Fill147 DSA SheetSolveEasy
- Number of Islands147 DSA SheetSolveMedium
- Max Area of Island (opens LeetCode)LeetCodeLCMedium
- Surrounded Regions147 DSA SheetSolveMedium
- Pacific Atlantic Water Flow147 DSA SheetSolveMedium
- Number of Distinct Islands (opens LeetCode)LeetCodeLCMedium
- Number of Enclaves (opens LeetCode)LeetCodeLCMedium
- Number of Closed Islands (opens LeetCode)LeetCodeLCMedium
Fewest steps with BFSEvery move costs the same and you want the fewest moves, often from many starting points at once.6
- Rotting Oranges147 DSA SheetSolveMedium
- 01 Matrix147 DSA SheetSolveMedium
- Walls and Gates (opens LeetCode)LeetCodeLCMedium
- Shortest Path in Binary Matrix (opens LeetCode)LeetCodeLCMedium
- Word Ladder147 DSA SheetSolveHard
- Shortest path with unit weights (opens GFG)GFGGFGMedium
Traversing a graphThe input is a list of edges or neighbours, not a grid.5
Cycle detectionYou need to know whether a path leads back to where it started.2
- Detecting cycles: parent tracking and the recursion stackVideo soonSoon
- Cycle in an undirected graph (opens GFG)GFGGFGMedium
- Cycle in a directed graph (opens GFG)GFGGFGMedium
Topological sortSome tasks must come before others, and you need a valid order or proof that none exists.7
- Topological sort: DFS order and Kahn's algorithmVideo soonSoon
- Course Schedule147 DSA SheetSolveMedium
- Course Schedule II147 DSA SheetSolveMedium
- Alien Dictionary (opens LeetCode)LeetCodeLCHard
- Topological sort (Kahn's algorithm) (opens GFG)GFGGFGMedium
- Find Eventual Safe States (opens LeetCode)LeetCodeLCMedium
- Course Schedule IV (opens LeetCode)LeetCodeLCMedium
- Minimum Height Trees (opens LeetCode)LeetCodeLCMedium
Union-findEdges arrive one at a time, and you keep asking whether two nodes are already connected.9
- Disjoint sets: union by rank and path compressionVideo soonSoon
- Redundant Connection147 DSA SheetSolveMedium
- Number of Operations to Make Network Connected (opens LeetCode)LeetCodeLCMedium
- Making A Large Island (opens LeetCode)LeetCodeLCHard
- Number of Islands II (opens LeetCode)LeetCodeLCHard
- Disjoint set union (opens GFG)GFGGFGMedium
- Accounts Merge (opens LeetCode)LeetCodeLCMedium
- Most Stones Removed with Same Row or Column (opens LeetCode)LeetCodeLCMedium
- Satisfiability of Equality Equations (opens LeetCode)LeetCodeLCMedium
- Redundant Connection II (opens LeetCode)LeetCodeLCHard
18Advanced Graphsafter Graphs, Heaps3 patterns · 6 lessons20
When edges have weights, plain BFS no longer finds the shortest path. These algorithms handle weights.
Weighted shortest pathsEdges have different costs and you want the cheapest route.14
- Dijkstra's algorithm and why it fails on negative edgesVideo soonSoon
- Bellman-Ford: relaxing every edge V−1 timesVideo soonSoon
- Floyd-Warshall: all pairs in O(V³)Video soonSoon
- Network Delay Time147 DSA SheetSolveMedium
- Path With Minimum Effort147 DSA SheetSolveMedium
- Cheapest Flights Within K Stops147 DSA SheetSolveMedium
- Swim in Rising Water147 DSA SheetSolveHard
- Find the City With the Smallest Number of Neighbors at a Threshold Distance (opens LeetCode)LeetCodeLCMedium
- Word Ladder II (opens LeetCode)LeetCodeLCHard
- Shortest path in a DAG (opens GFG)GFGGFGMedium
- Dijkstra's algorithm (opens GFG)GFGGFGMedium
- Print the shortest path (opens GFG)GFGGFGMedium
- Number of Ways to Arrive at Destination (opens LeetCode)LeetCodeLCMedium
- Path with Maximum Probability (opens LeetCode)LeetCodeLCMedium
- Minimum multiplications to reach the end (opens GFG)GFGGFGHard
- Bellman-Ford algorithm (opens GFG)GFGGFGMedium
- Floyd-Warshall algorithm (opens GFG)GFGGFGHard
Minimum spanning treesYou must connect every node at the lowest total cost.2
- Minimum spanning trees: Prim's and Kruskal'sVideo soonSoon
- Min Cost to Connect All Points147 DSA SheetSolveMedium
- Minimum spanning tree weight (opens GFG)GFGGFGMedium
Special edges and pathsYou must use every edge once, or find the edges whose removal splits the graph.4
- Strongly connected componentsVideo soonSoon
- Bridges and articulation points (Tarjan)Video soonSoon
- Reconstruct Itinerary (opens LeetCode)LeetCodeLCHard
- Critical Connections in a Network (opens LeetCode)LeetCodeLCHard
- Strongly connected components (Kosaraju) (opens GFG)GFGGFGHard
- Articulation points (opens GFG)GFGGFGHard
19Greedyafter Sorting1 pattern · 1 lesson15
A greedy solution takes the best-looking choice at each step and never goes back. The hard part is proving that this is safe.
The best choice nowA choice that looks best now can never hurt you later.15
- When greedy works: the exchange argumentVideo soonSoon
- Jump Game147 DSA SheetSolveMedium
- Jump Game II147 DSA SheetSolveMedium
- Gas Station147 DSA SheetSolveMedium
- Partition Labels147 DSA SheetSolveMedium
- Hand of Straights (opens LeetCode)LeetCodeLCMedium
- Merge Triplets to Form Target Triplet (opens LeetCode)LeetCodeLCMedium
- Valid Parenthesis String147 DSA SheetSolveMedium
- Candy147 DSA SheetSolveHard
- Assign Cookies (opens LeetCode)LeetCodeLCEasy
- Lemonade Change (opens LeetCode)LeetCodeLCEasy
- Fractional knapsack (opens GFG)GFGGFGMedium
- Fewest coins with greedy (opens GFG)GFGGFGEasy
- Shortest job first (opens GFG)GFGGFGMedium
- Job sequencing (opens GFG)GFGGFGMedium
- Minimum Number of Arrows to Burst Balloons (opens LeetCode)LeetCodeLCMedium
20Intervalsafter Greedy1 pattern · 1 lesson8
Sort the intervals by start or by end first. After that, most interval problems take one pass.
Merging and schedulingThe input is a list of start and end times.8
- Sort by start or by end: which one and whyVideo soonSoon
- Merge Intervals147 DSA SheetSolveMedium
- Insert Interval147 DSA SheetSolveMedium
- Non-overlapping Intervals147 DSA SheetSolveMedium
- Meeting Rooms (opens LeetCode)LeetCodeLCEasy
- Meeting Rooms II (opens LeetCode)LeetCodeLCMedium
- Minimum Interval to Include Each Query147 DSA SheetSolveHard
- Most meetings in one room (opens GFG)GFGGFGEasy
- Minimum platforms (opens GFG)GFGGFGMedium
211-D Dynamic Programmingafter Recursion and Backtracking4 patterns · 3 lessons24
Dynamic programming is recursion that remembers answers it has already worked out. In 1-D problems, each answer depends on a few earlier ones.
Take it or skip itAt each position you either use it or skip it, and the best answer builds on the ones before.11
- From recursion to memo to tableVideo soonSoon
- Keeping only the last few statesVideo soonSoon
- Climbing Stairs147 DSA SheetSolveEasy
- Min Cost Climbing Stairs (opens LeetCode)LeetCodeLCEasy
- House Robber147 DSA SheetSolveMedium
- House Robber II147 DSA SheetSolveMedium
- Decode Ways147 DSA SheetSolveMedium
- Word Break147 DSA SheetSolveMedium
- N-th Tribonacci Number (opens LeetCode)LeetCodeLCEasy
- Frog jump with K steps (opens GFG)GFGGFGEasy
- Perfect Squares (opens LeetCode)LeetCodeLCMedium
- Minimum Cost For Tickets (opens LeetCode)LeetCodeLCMedium
- Training schedule with no repeated activity (opens GFG)GFGGFGMedium
PalindromesThe question asks about palindromic substrings.2
- Longest Palindromic Substring147 DSA SheetSolveMedium
- Palindromic Substrings147 DSA SheetSolveMedium
Increasing subsequencesThe answer is a subsequence that must keep going up.6
- LIS in O(n log n) with binary searchVideo soonSoon
- Longest Increasing Subsequence147 DSA SheetSolveMedium
- Number of Longest Increasing Subsequence (opens LeetCode)LeetCodeLCMedium
- Print a longest increasing subsequence (opens GFG)GFGGFGHard
- Largest Divisible Subset (opens LeetCode)LeetCodeLCMedium
- Longest String Chain (opens LeetCode)LeetCodeLCMedium
- Longest bitonic subsequence (opens GFG)GFGGFGMedium
Buying and selling with statesYou either hold a stock or you do not, and the rules limit how you switch.5
- Best Time to Buy and Sell Stock with Cooldown147 DSA SheetSolveMedium
- Best Time to Buy and Sell Stock with Transaction Fee (opens LeetCode)LeetCodeLCMedium
- Best Time to Buy and Sell Stock IV (opens LeetCode)LeetCodeLCHard
- Best Time to Buy and Sell Stock II (opens LeetCode)LeetCodeLCMedium
- Best Time to Buy and Sell Stock III (opens LeetCode)LeetCodeLCHard
222-D Dynamic Programmingafter 1-D Dynamic Programming4 patterns · 4 lessons40
Here each answer depends on two indexes, such as a row and a column, or a position in each of two strings.
Paths in a gridYou move through a grid and count the paths or find the best one.8
- DP on grids: the state is the cellVideo soonSoon
- Unique Paths147 DSA SheetSolveMedium
- Unique Paths II (opens LeetCode)LeetCodeLCMedium
- Longest Increasing Path in a Matrix147 DSA SheetSolveHard
- Minimum Path Sum (opens LeetCode)LeetCodeLCMedium
- Triangle (opens LeetCode)LeetCodeLCMedium
- Minimum Falling Path Sum (opens LeetCode)LeetCodeLCMedium
- Count Square Submatrices with All Ones (opens LeetCode)LeetCodeLCMedium
- Cherry Pickup II (opens LeetCode)LeetCodeLCHard
KnapsackYou pick items to reach a target sum or fill a capacity.12
- 0/1 and unbounded knapsackVideo soonSoon
- 0/1 knapsack (opens GFG)GFGGFGMedium
- Partition Equal Subset Sum147 DSA SheetSolveMedium
- Coin Change147 DSA SheetSolveMedium
- Coin Change II147 DSA SheetSolveMedium
- Target Sum147 DSA SheetSolveMedium
- Last Stone Weight II (opens LeetCode)LeetCodeLCMedium
- Subset sum (opens GFG)GFGGFGMedium
- Count subsets with sum K (opens GFG)GFGGFGMedium
- Count partitions with a given difference (opens GFG)GFGGFGMedium
- Unbounded knapsack (opens GFG)GFGGFGMedium
- Rod cutting (opens GFG)GFGGFGHard
- Partition Array Into Two Arrays to Minimize Sum Difference (opens LeetCode)LeetCodeLCHard
Two stringsYou compare two strings one character at a time.14
- DP on two strings: i, j and what matchesVideo soonSoon
- Longest Common Subsequence147 DSA SheetSolveMedium
- Maximum Length of Repeated Subarray (opens LeetCode)LeetCodeLCMedium
- Edit Distance147 DSA SheetSolveMedium
- Longest Palindromic Subsequence (opens LeetCode)LeetCodeLCMedium
- Distinct Subsequences (opens LeetCode)LeetCodeLCHard
- Interleaving String (opens LeetCode)LeetCodeLCMedium
- Wildcard Matching (opens LeetCode)LeetCodeLCHard
- Regular Expression Matching (opens LeetCode)LeetCodeLCHard
- Print a longest common subsequence (opens GFG)GFGGFGMedium
- Delete Operation for Two Strings (opens LeetCode)LeetCodeLCMedium
- Minimum Insertion Steps to Make a String Palindrome (opens LeetCode)LeetCodeLCHard
- Shortest Common Supersequence (opens LeetCode)LeetCodeLCHard
- Count palindromic subsequences (opens GFG)GFGGFGHard
- Minimum ASCII Delete Sum for Two Strings (opens LeetCode)LeetCodeLCMedium
Splitting a rangeYou choose where to split a range, and each side becomes a smaller problem.6
- Partition DP: try every split pointVideo soonSoon
- Burst Balloons147 DSA SheetSolveHard
- Palindrome Partitioning II (opens LeetCode)LeetCodeLCHard
- Matrix chain multiplication (opens GFG)GFGGFGHard
- Minimum Cost to Cut a Stick (opens LeetCode)LeetCodeLCHard
- Partition Array for Maximum Sum (opens LeetCode)LeetCodeLCMedium
- Ways to evaluate a boolean expression to true (opens GFG)GFGGFGHard
23String Matchingafter Strings1 pattern · 3 lessons6
These algorithms find a pattern inside a text faster than checking every starting position.
Pattern matchingYou must find a pattern inside a longer string, and checking every start is too slow.6
- KMP and the LPS arrayVideo soonSoon
- The Z-functionVideo soonSoon
- Rolling hashes and Rabin-KarpVideo soonSoon
- Find the Index of the First Occurrence in a String147 DSA SheetSolveEasy
- Shortest Palindrome (opens LeetCode)LeetCodeLCHard
- Rabin-Karp (opens GFG)GFGGFGMedium
- Z-function (opens GFG)GFGGFGMedium
- Repeated String Match (opens LeetCode)LeetCodeLCMedium
- Longest Happy Prefix (opens LeetCode)LeetCodeLCHard