Two Pointers LeetCode Problems: All 201, With Python Solutions
Every problem in this library that LeetCode tags Two Pointers — 201 in total, 169 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 201 problems
- 58 Easy
- 118 Medium
- 25 Hard
How Two Pointers problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Two Pointers problems — each one explains the approach, gives a Python template and states its complexity.
- Two Pointers — Use the order already in the input to discard half the search space at every step.
Two Pointers problems by difficulty
Showing the first 200 of 201 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (58)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 26 | Remove Duplicates from Sorted Array | Easy | Array, Two Pointers |
| 27 | Remove Element | Easy | Array, Two Pointers |
| 28 | Find the Index of the First Occurrence in a String | Easy | Two Pointers, String, String Matching |
| 88 | Merge Sorted Array | Easy | Array, Two Pointers, Sorting |
| 125 | Valid Palindrome | Easy | Two Pointers, String |
| 141 | Linked List Cycle | Easy | Hash Table, Linked List, Two Pointers |
| 160 | Intersection of Two Linked Lists | Easy | Hash Table, Linked List, Two Pointers |
| 202 | Happy Number | Easy | Hash Table, Math, Two Pointers |
| 234 | Palindrome Linked List | Easy | Stack, Recursion, Linked List +1 |
| 283 | Move Zeroes | Easy | Array, Two Pointers |
| 344 | Reverse String | Easy | Two Pointers, String |
| 345 | Reverse Vowels of a String | Easy | Two Pointers, String |
| 349 | Intersection of Two Arrays | Easy | Array, Hash Table, Two Pointers +2 |
| 350 | Intersection of Two Arrays II | Easy | Array, Hash Table, Two Pointers +2 |
| 392 | Is Subsequence | Easy | Two Pointers, String, Dynamic Programming |
| 455 | Assign Cookies | Easy | Greedy, Array, Two Pointers +1 |
| 541 | Reverse String II | Easy | Two Pointers, String |
| 557 | Reverse Words in a String III | Easy | Two Pointers, String |
| 653 | Two Sum IV - Input is a BST | Easy | Tree, Depth-First Search, Breadth-First Search +4 |
| 680 | Valid Palindrome II | Easy | Greedy, Two Pointers, String |
| 696 | Count Binary Substrings | Easy | Two Pointers, String |
| 821 | Shortest Distance to a Character | Easy | Array, Two Pointers, String |
| 832 | Flipping an Image | Easy | Bit Manipulation, Array, Two Pointers +2 |
| 844 | Backspace String Compare | Easy | Stack, Two Pointers, String +1 |
| 876 | Middle of the Linked List | Easy | Linked List, Two Pointers |
| 905 | Sort Array By Parity | Easy | Array, Two Pointers, Sorting |
| 917 | Reverse Only Letters | Easy | Two Pointers, String |
| 922 | Sort Array By Parity II | Easy | Array, Two Pointers, Sorting |
| 925 | Long Pressed Name | Easy | Two Pointers, String |
| 942 | DI String Match | Easy | Greedy, Array, Two Pointers +1 |
| 977 | Squares of a Sorted Array | Easy | Array, Two Pointers, Sorting |
| 1089 | Duplicate Zeros | Easy | Array, Two Pointers |
| 1332 | Remove Palindromic Subsequences | Easy | Two Pointers, String |
| 1346 | Check If N and Its Double Exist | Easy | Array, Hash Table, Two Pointers +2 |
| 1385 | Find the Distance Value Between Two Arrays | Easy | Array, Two Pointers, Binary Search +1 |
| 1455 | Check If a Word Occurs As a Prefix of Any Word in a Sentence | Easy | Two Pointers, String, String Matching |
| 1768 | Merge Strings Alternately | Easy | Two Pointers, String |
| 1961 | Check If String Is a Prefix of Array | Easy | Array, Two Pointers, String |
| 2000 | Reverse Prefix of Word | Easy | Stack, Two Pointers, String |
| 2108 | Find First Palindromic String in the Array | Easy | Array, Two Pointers, String |
| 2200 | Find All K-Distant Indices in an Array | Easy | Array, Two Pointers |
| 2367 | Number of Arithmetic Triplets | Easy | Array, Hash Table, Two Pointers +1 |
| 2441 | Largest Positive Integer That Exists With Its Negative | Easy | Array, Hash Table, Two Pointers +1 |
| 2460 | Apply Operations to an Array | Easy | Array, Two Pointers, Simulation |
| 2465 | Number of Distinct Averages | Easy | Array, Hash Table, Two Pointers +1 |
| 2511 | Maximum Enemy Forts That Can Be Captured | Easy | Array, Two Pointers |
| 2540 | Minimum Common Value | Easy | Array, Hash Table, Two Pointers +1 |
| 2562 | Find the Array Concatenation Value | Easy | Array, Two Pointers, Simulation |
| 2570 | Merge Two 2D Arrays by Summing Values | Easy | Array, Hash Table, Two Pointers |
| 2697 | Lexicographically Smallest Palindrome | Easy | Greedy, Two Pointers, String |
| 2824 | Count Pairs Whose Sum is Less than Target | Easy | Array, Two Pointers, Binary Search +1 |
| 2903 | Find Indices With Index and Value Difference I | Easy | Array, Two Pointers |
| 2970 | Count the Number of Incremovable Subarrays I | Easy | Array, Two Pointers, Binary Search +1 |
| 170 | Two Sum III - Data structure designPremium | Easy | Design, Array, Hash Table +2 |
| 246 | Strobogrammatic NumberPremium | Easy | Hash Table, Two Pointers, String |
| 408 | Valid Word AbbreviationPremium | Easy | Two Pointers, String |
| 1099 | Two Sum Less Than KPremium | Easy | Array, Two Pointers, Binary Search +1 |
| 1826 | Faulty SensorPremium | Easy | Array, Two Pointers |
Medium (118)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 5 | Longest Palindromic Substring | Medium | Two Pointers, String, Dynamic Programming |
| 11 | Container With Most Water | Medium | Greedy, Array, Two Pointers |
| 15 | 3Sum | Medium | Array, Two Pointers, Sorting |
| 16 | 3Sum Closest | Medium | Array, Two Pointers, Sorting |
| 18 | 4Sum | Medium | Array, Two Pointers, Sorting |
| 19 | Remove Nth Node From End of List | Medium | Linked List, Two Pointers |
| 31 | Next Permutation | Medium | Array, Two Pointers |
| 61 | Rotate List | Medium | Linked List, Two Pointers |
| 75 | Sort Colors | Medium | Array, Two Pointers, Sorting |
| 80 | Remove Duplicates from Sorted Array II | Medium | Array, Two Pointers |
| 82 | Remove Duplicates from Sorted List II | Medium | Linked List, Two Pointers |
| 86 | Partition List | Medium | Linked List, Two Pointers |
| 142 | Linked List Cycle II | Medium | Hash Table, Linked List, Two Pointers |
| 143 | Reorder List | Medium | Stack, Recursion, Linked List +1 |
| 148 | Sort List | Medium | Linked List, Two Pointers, Divide and Conquer +2 |
| 151 | Reverse Words in a String | Medium | Two Pointers, String |
| 165 | Compare Version Numbers | Medium | Two Pointers, String |
| 167 | Two Sum II - Input Array Is Sorted | Medium | Array, Two Pointers, Binary Search |
| 189 | Rotate Array | Medium | Array, Math, Two Pointers |
| 287 | Find the Duplicate Number | Medium | Bit Manipulation, Array, Two Pointers +1 |
| 443 | String Compression | Medium | Two Pointers, String |
| 457 | Circular Array Loop | Medium | Array, Hash Table, Two Pointers |
| 475 | Heaters | Medium | Array, Two Pointers, Binary Search +1 |
| 481 | Magical String | Medium | Two Pointers, String |
| 522 | Longest Uncommon Subsequence II | Medium | Array, Hash Table, Two Pointers +2 |
| 524 | Longest Word in Dictionary through Deleting | Medium | Array, Two Pointers, String +1 |
| 532 | K-diff Pairs in an Array | Medium | Array, Hash Table, Two Pointers +2 |
| 556 | Next Greater Element III | Medium | Math, Two Pointers, String |
| 567 | Permutation in String | Medium | Hash Table, Two Pointers, String +1 |
| 581 | Shortest Unsorted Continuous Subarray | Medium | Stack, Greedy, Array +3 |
| 611 | Valid Triangle Number | Medium | Greedy, Array, Two Pointers +2 |
| 633 | Sum of Square Numbers | Medium | Math, Two Pointers, Binary Search |
| 647 | Palindromic Substrings | Medium | Two Pointers, String, Dynamic Programming |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 763 | Partition Labels | Medium | Greedy, Hash Table, Two Pointers +1 |
| 777 | Swap Adjacent in LR String | Medium | Two Pointers, String |
| 786 | K-th Smallest Prime Fraction | Medium | Array, Two Pointers, Binary Search +2 |
| 795 | Number of Subarrays with Bounded Maximum | Medium | Array, Two Pointers |
| 809 | Expressive Words | Medium | Array, Two Pointers, String |
| 825 | Friends Of Appropriate Ages | Medium | Array, Two Pointers, Binary Search +1 |
| 826 | Most Profit Assigning Work | Medium | Greedy, Array, Two Pointers +2 |
| 838 | Push Dominoes | Medium | Two Pointers, String, Dynamic Programming |
| 845 | Longest Mountain in Array | Medium | Array, Two Pointers, Dynamic Programming +1 |
| 870 | Advantage Shuffle | Medium | Greedy, Array, Two Pointers +1 |
| 881 | Boats to Save People | Medium | Greedy, Array, Two Pointers +1 |
| 923 | 3Sum With Multiplicity | Medium | Array, Hash Table, Two Pointers +2 |
| 948 | Bag of Tokens | Medium | Greedy, Array, Two Pointers +1 |
| 962 | Maximum Width Ramp | Medium | Stack, Array, Two Pointers +1 |
| 969 | Pancake Sorting | Medium | Greedy, Array, Two Pointers +1 |
| 986 | Interval List Intersections | Medium | Array, Two Pointers, Line Sweep |
| 1023 | Camelcase Matching | Medium | Trie, Array, Two Pointers +2 |
| 1048 | Longest String Chain | Medium | Array, Hash Table, Two Pointers +3 |
| 1237 | Find Positive Integer Solution for a Given Equation | Medium | Math, Two Pointers, Binary Search +1 |
| 1471 | The k Strongest Values in an Array | Medium | Array, Two Pointers, Sorting |
| 1498 | Number of Subsequences That Satisfy the Given Sum Condition | Medium | Array, Two Pointers, Binary Search +1 |
| 1508 | Range Sum of Sorted Subarray Sums | Medium | Array, Two Pointers, Binary Search +2 |
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Stack, Array, Two Pointers +2 |
| 1577 | Number of Ways Where Square of Number Is Equal to Product of Two Numbers | Medium | Array, Hash Table, Math +1 |
| 1616 | Split Two Strings to Make Palindrome | Medium | Two Pointers, String |
| 1679 | Max Number of K-Sum Pairs | Medium | Array, Hash Table, Two Pointers +1 |
| 1712 | Ways to Split Array Into Three Subarrays | Medium | Array, Two Pointers, Binary Search +1 |
| 1721 | Swapping Nodes in a Linked List | Medium | Linked List, Two Pointers |
| 1750 | Minimum Length of String After Deleting Similar Ends | Medium | Two Pointers, String |
| 1754 | Largest Merge Of Two Strings | Medium | Greedy, Two Pointers, String |
| 1764 | Form Array by Concatenating Subarrays of Another Array | Medium | Greedy, Array, Two Pointers +1 |
| 1813 | Sentence Similarity III | Medium | Array, Two Pointers, String |
| 1850 | Minimum Adjacent Swaps to Reach the Kth Smallest Number | Medium | Greedy, Two Pointers, String |
| 1855 | Maximum Distance Between a Pair of Values | Medium | Array, Two Pointers, Binary Search |
| 1861 | Rotating the Box | Medium | Array, Two Pointers, Matrix |
| 1877 | Minimize Maximum Pair Sum in Array | Medium | Greedy, Array, Two Pointers +1 |
| 1898 | Maximum Number of Removable Characters | Medium | Array, Two Pointers, String +1 |
| 1963 | Minimum Number of Swaps to Make the String Balanced | Medium | Stack, Greedy, Two Pointers +1 |
| 2095 | Delete the Middle Node of a Linked List | Medium | Linked List, Two Pointers |
| 2105 | Watering Plants II | Medium | Array, Two Pointers, Simulation |
| 2109 | Adding Spaces to a String | Medium | Array, Two Pointers, String +1 |
| 2110 | Number of Smooth Descent Periods of a Stock | Medium | Array, Math, Two Pointers +2 |
| 2130 | Maximum Twin Sum of a Linked List | Medium | Stack, Linked List, Two Pointers |
| 2149 | Rearrange Array Elements by Sign | Medium | Array, Two Pointers, Simulation |
| 2161 | Partition Array According to Given Pivot | Medium | Array, Two Pointers, Simulation |
| 2300 | Successful Pairs of Spells and Potions | Medium | Array, Two Pointers, Binary Search +1 |
| 2332 | The Latest Time to Catch a Bus | Medium | Array, Two Pointers, Binary Search +1 |
| 2337 | Move Pieces to Obtain a String | Medium | Two Pointers, String |
| 2396 | Strictly Palindromic Number | Medium | Brainteaser, Math, Two Pointers |
| 2406 | Divide Intervals Into Minimum Number of Groups | Medium | Greedy, Array, Two Pointers +3 |
| 2410 | Maximum Matching of Players With Trainers | Medium | Greedy, Array, Two Pointers +1 |
| 2462 | Total Cost to Hire K Workers | Medium | Array, Two Pointers, Simulation +1 |
| 2486 | Append Characters to String to Make Subsequence | Medium | Greedy, Two Pointers, String |
| 2491 | Divide Players Into Teams of Equal Skill | Medium | Array, Hash Table, Two Pointers +1 |
| 2563 | Count the Number of Fair Pairs | Medium | Array, Two Pointers, Binary Search +1 |
| 2576 | Find the Maximum Number of Marked Indices | Medium | Greedy, Array, Two Pointers +2 |
| 2592 | Maximize Greatness of an Array | Medium | Greedy, Array, Two Pointers +1 |
| 2825 | Make String a Subsequence Using Cyclic Increments | Medium | Two Pointers, String |
| 2856 | Minimum Array Length After Pair Removals | Medium | Greedy, Array, Hash Table +3 |
| 2905 | Find Indices With Index and Value Difference II | Medium | Array, Two Pointers |
| 2938 | Separate Black and White Balls | Medium | Greedy, Two Pointers, String |
| 161 | One Edit DistancePremium | Medium | Two Pointers, String |
| 186 | Reverse Words in a String IIPremium | Medium | Two Pointers, String |
| 244 | Shortest Word Distance IIPremium | Medium | Design, Array, Hash Table +2 |
| 251 | Flatten 2D VectorPremium | Medium | Design, Array, Two Pointers +1 |
| 253 | Meeting Rooms IIPremium | Medium | Greedy, Array, Two Pointers +3 |
| 259 | 3Sum SmallerPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 277 | Find the CelebrityPremium | Medium | Graph, Two Pointers, Interactive |
| 360 | Sort Transformed ArrayPremium | Medium | Array, Math, Two Pointers +1 |
| 723 | Candy CrushPremium | Medium | Array, Two Pointers, Matrix +1 |
| 1055 | Shortest Way to Form StringPremium | Medium | Greedy, Two Pointers, String +1 |
| 1214 | Two Sum BSTsPremium | Medium | Stack, Tree, Depth-First Search +4 |
| 1229 | Meeting SchedulerPremium | Medium | Array, Two Pointers, Sorting |
| 1265 | Print Immutable Linked List in ReversePremium | Medium | Stack, Recursion, Linked List +1 |
| 1570 | Dot Product of Two Sparse VectorsPremium | Medium | Design, Array, Hash Table +1 |
| 1634 | Add Two Polynomials Represented as Linked ListsPremium | Medium | Linked List, Math, Two Pointers |
| 1650 | Lowest Common Ancestor of a Binary Tree IIIPremium | Medium | Tree, Hash Table, Two Pointers +1 |
| 1868 | Product of Two Run-Length Encoded ArraysPremium | Medium | Array, Two Pointers |
| 1885 | Count Pairs in Two ArraysPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 2046 | Sort Linked List Already Sorted Using Absolute ValuesPremium | Medium | Linked List, Two Pointers, Sorting |
| 2330 | Valid Palindrome IVPremium | Medium | Two Pointers, String |
| 2422 | Merge Operations to Turn Array Into a PalindromePremium | Medium | Greedy, Array, Two Pointers |
| 2674 | Split a Circular Linked ListPremium | Medium | Linked List, Two Pointers |
| 2838 | Maximum Coins Heroes Can CollectPremium | Medium | Array, Two Pointers, Binary Search +2 |
Hard (24)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 42 | Trapping Rain Water | Hard | Stack, Array, Two Pointers +2 |
| 295 | Find Median from Data Stream | Hard | Design, Two Pointers, Data Stream +2 |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 719 | Find K-th Smallest Pair Distance | Hard | Array, Two Pointers, Binary Search +1 |
| 1147 | Longest Chunked Palindrome Decomposition | Hard | Greedy, Two Pointers, String +3 |
| 1163 | Last Substring in Lexicographical Order | Hard | Two Pointers, String |
| 1537 | Get the Maximum Score | Hard | Greedy, Array, Two Pointers +1 |
| 1697 | Checking Existence of Edge Length Limited Paths | Hard | Union Find, Graph, Array +2 |
| 1755 | Closest Subsequence Sum | Hard | Bit Manipulation, Array, Two Pointers +3 |
| 1782 | Count Pairs Of Nodes | Hard | Graph, Array, Hash Table +4 |
| 1793 | Maximum Score of a Good Subarray | Hard | Stack, Array, Two Pointers +2 |
| 2035 | Partition Array Into Two Arrays to Minimize Sum Difference | Hard | Bit Manipulation, Array, Two Pointers +4 |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2122 | Recover the Original Array | Hard | Array, Hash Table, Two Pointers +2 |
| 2193 | Minimum Number of Moves to Make Palindrome | Hard | Greedy, Binary Indexed Tree, Two Pointers +1 |
| 2234 | Maximum Total Beauty of the Gardens | Hard | Greedy, Array, Two Pointers +4 |
| 2472 | Maximum Number of Non-overlapping Palindrome Substrings | Hard | Greedy, Two Pointers, String +1 |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2565 | Subsequence With the Minimum Score | Hard | Two Pointers, String, Binary Search |
| 2911 | Minimum Changes to Make K Semi-palindromes | Hard | Two Pointers, String, Dynamic Programming |
| 2972 | Count the Number of Incremovable Subarrays II | Hard | Array, Two Pointers, Binary Search |
| 272 | Closest Binary Search Tree Value IIPremium | Hard | Stack, Tree, Depth-First Search +4 |
| 1842 | Next Palindrome Using Same DigitsPremium | Hard | Two Pointers, String |
| 2604 | Minimum Time to Eat All GrainsPremium | Hard | Array, Two Pointers, Binary Search +1 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Two Pointers 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.