Counting LeetCode Problems: All 126, With Python Solutions
Every problem in this library that LeetCode tags Counting — 126 in total, 112 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 126 problems
- 47 Easy
- 65 Medium
- 14 Hard
How Counting problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Counting 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.
Counting problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (47)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 169 | Majority Element | Easy | Array, Hash Table, Divide and Conquer +2 |
| 383 | Ransom Note | Easy | Hash Table, String, Counting |
| 387 | First Unique Character in a String | Easy | Queue, Hash Table, String +1 |
| 594 | Longest Harmonious Subsequence | Easy | Array, Hash Table, Counting +2 |
| 819 | Most Common Word | Easy | Array, Hash Table, String +1 |
| 884 | Uncommon Words from Two Sentences | Easy | Hash Table, String, Counting |
| 914 | X of a Kind in a Deck of Cards | Easy | Array, Hash Table, Math +2 |
| 1128 | Number of Equivalent Domino Pairs | Easy | Array, Hash Table, Counting |
| 1160 | Find Words That Can Be Formed by Characters | Easy | Array, Hash Table, String +1 |
| 1189 | Maximum Number of Balloons | Easy | Hash Table, String, Counting |
| 1221 | Split a String in Balanced Strings | Easy | Greedy, String, Counting |
| 1356 | Sort Integers by The Number of 1 Bits | Easy | Bit Manipulation, Array, Counting +1 |
| 1370 | Increasing Decreasing String | Easy | Hash Table, String, Counting |
| 1394 | Find Lucky Integer in an Array | Easy | Array, Hash Table, Counting |
| 1512 | Number of Good Pairs | Easy | Array, Hash Table, Math +1 |
| 1603 | Design Parking System | Easy | Design, Counting, Simulation |
| 1684 | Count the Number of Consistent Strings | Easy | Bit Manipulation, Array, Hash Table +2 |
| 1704 | Determine if String Halves Are Alike | Easy | String, Counting |
| 1742 | Maximum Number of Balls in a Box | Easy | Hash Table, Math, Counting |
| 1748 | Sum of Unique Elements | Easy | Array, Hash Table, Counting |
| 1790 | Check if One String Swap Can Make Strings Equal | Easy | Hash Table, String, Counting |
| 1854 | Maximum Population Year | Easy | Array, Counting, Prefix Sum |
| 1876 | Substrings of Size Three with Distinct Characters | Easy | Hash Table, String, Counting +1 |
| 1897 | Redistribute Characters to Make All Strings Equal | Easy | Hash Table, String, Counting |
| 1941 | Check if All Characters Have Equal Number of Occurrences | Easy | Hash Table, String, Counting |
| 2006 | Count Number of Pairs With Absolute Difference K | Easy | Array, Hash Table, Counting |
| 2053 | Kth Distinct String in an Array | Easy | Array, Hash Table, String +1 |
| 2068 | Check Whether Two Strings are Almost Equivalent | Easy | Hash Table, String, Counting |
| 2085 | Count Common Words With One Occurrence | Easy | Array, Hash Table, String +1 |
| 2148 | Count Elements With Strictly Smaller and Greater Elements | Easy | Array, Counting, Sorting |
| 2190 | Most Frequent Number Following Key In an Array | Easy | Array, Hash Table, Counting |
| 2206 | Divide Array Into Equal Pairs | Easy | Bit Manipulation, Array, Hash Table +1 |
| 2248 | Intersection of Multiple Arrays | Easy | Array, Hash Table, Counting +1 |
| 2283 | Check if Number Has Equal Digit Count and Digit Value | Easy | Hash Table, String, Counting |
| 2287 | Rearrange Characters to Make Target String | Easy | Hash Table, String, Counting |
| 2341 | Maximum Number of Pairs in Array | Easy | Array, Hash Table, Counting |
| 2347 | Best Poker Hand | Easy | Array, Hash Table, Counting |
| 2351 | First Letter to Appear Twice | Easy | Bit Manipulation, Hash Table, String +1 |
| 2404 | Most Frequent Even Element | Easy | Array, Hash Table, Counting |
| 2423 | Remove Letter To Equalize Frequency | Easy | Hash Table, String, Counting |
| 2506 | Count Pairs Of Similar Strings | Easy | Bit Manipulation, Array, Hash Table +2 |
| 2529 | Maximum Count of Positive Integer and Negative Integer | Easy | Array, Binary Search, Counting |
| 2586 | Count the Number of Vowel Strings in Range | Easy | Array, String, Counting |
| 2748 | Number of Beautiful Pairs | Easy | Array, Hash Table, Math +2 |
| 2833 | Furthest Point From Origin | Easy | String, Counting |
| 2960 | Count Tested Devices After Test Operations | Easy | Array, Counting, Simulation |
| 1213 | Intersection of Three Sorted ArraysPremium | Easy | Array, Hash Table, Binary Search +1 |
Medium (65)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 229 | Majority Element II | Medium | Array, Hash Table, Counting +1 |
| 299 | Bulls and Cows | Medium | Hash Table, String, Counting |
| 347 | Top K Frequent Elements | Medium | Array, Hash Table, Divide and Conquer +5 |
| 451 | Sort Characters By Frequency | Medium | Hash Table, String, Bucket Sort +3 |
| 621 | Task Scheduler | Medium | Greedy, Array, Hash Table +3 |
| 692 | Top K Frequent Words | Medium | Trie, Array, Hash Table +5 |
| 767 | Reorganize String | Medium | Greedy, Hash Table, String +3 |
| 811 | Subdomain Visit Count | Medium | Array, Hash Table, String +1 |
| 869 | Reordered Power of 2 | Medium | Hash Table, Math, Counting +2 |
| 900 | RLE Iterator | Medium | Design, Array, Counting +1 |
| 923 | 3Sum With Multiplicity | Medium | Array, Hash Table, Two Pointers +2 |
| 945 | Minimum Increment to Make Array Unique | Medium | Greedy, Array, Counting +1 |
| 1010 | Pairs of Songs With Total Durations Divisible by 60 | Medium | Array, Hash Table, Counting |
| 1054 | Distant Barcodes | Medium | Greedy, Array, Hash Table +3 |
| 1079 | Letter Tile Possibilities | Medium | Hash Table, String, Backtracking +1 |
| 1090 | Largest Values From Labels | Medium | Greedy, Array, Hash Table +2 |
| 1267 | Count Servers that Communicate | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 1347 | Minimum Number of Steps to Make Two Strings Anagram | Medium | Hash Table, String, Counting |
| 1366 | Rank Teams by Votes | Medium | Array, Hash Table, String +2 |
| 1400 | Construct K Palindrome Strings | Medium | Greedy, Hash Table, String +1 |
| 1419 | Minimum Number of Frogs Croaking | Medium | String, Counting |
| 1481 | Least Number of Unique Integers after K Removals | Medium | Greedy, Array, Hash Table +2 |
| 1497 | Check If Array Pairs Are Divisible by k | Medium | Array, Hash Table, Counting |
| 1519 | Number of Nodes in the Sub-Tree With the Same Label | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1657 | Determine if Two Strings Are Close | Medium | Hash Table, String, Counting +1 |
| 1726 | Tuple with Same Product | Medium | Array, Hash Table, Counting |
| 1737 | Change Minimum Characters to Satisfy One of Three Conditions | Medium | Hash Table, String, Counting +1 |
| 1775 | Equal Sum Arrays With Minimum Number of Operations | Medium | Greedy, Array, Hash Table +1 |
| 1781 | Sum of Beauty of All Substrings | Medium | Hash Table, String, Counting |
| 1814 | Count Nice Pairs in an Array | Medium | Array, Hash Table, Math +1 |
| 2001 | Number of Pairs of Interchangeable Rectangles | Medium | Array, Hash Table, Math +2 |
| 2013 | Detect Squares | Medium | Design, Array, Hash Table +1 |
| 2023 | Number of Pairs of Strings With Concatenation Equal to Target | Medium | Array, Hash Table, String +1 |
| 2029 | Stone Game IX | Medium | Greedy, Array, Math +2 |
| 2048 | Next Greater Numerically Balanced Number | Medium | Hash Table, Math, Backtracking +2 |
| 2131 | Longest Palindrome by Concatenating Two Letter Words | Medium | Greedy, Array, Hash Table +2 |
| 2150 | Find All Lonely Numbers in the Array | Medium | Array, Hash Table, Counting |
| 2170 | Minimum Operations to Make the Array Alternating | Medium | Greedy, Array, Hash Table +1 |
| 2182 | Construct String With Repeat Limit | Medium | Greedy, Hash Table, String +2 |
| 2186 | Minimum Number of Steps to Make Two Strings Anagram II | Medium | Hash Table, String, Counting |
| 2225 | Find Players With Zero or One Losses | Medium | Array, Hash Table, Counting +1 |
| 2244 | Minimum Rounds to Complete All Tasks | Medium | Greedy, Array, Hash Table +1 |
| 2275 | Largest Combination With Bitwise AND Greater Than Zero | Medium | Bit Manipulation, Array, Hash Table +1 |
| 2284 | Sender With Largest Word Count | Medium | Array, Hash Table, String +1 |
| 2364 | Count Number of Bad Pairs | Medium | Array, Hash Table, Math +1 |
| 2384 | Largest Palindromic Number | Medium | Greedy, Hash Table, String +1 |
| 2442 | Count Number of Distinct Integers After Reverse Operations | Medium | Array, Hash Table, Math +1 |
| 2453 | Destroy Sequential Targets | Medium | Array, Hash Table, Counting |
| 2526 | Find Consecutive Integers from a Data Stream | Medium | Design, Queue, Hash Table +2 |
| 2531 | Make Number of Distinct Characters Equal | Medium | Hash Table, String, Counting |
| 2856 | Minimum Array Length After Pair Removals | Medium | Greedy, Array, Hash Table +3 |
| 2870 | Minimum Number of Operations to Make Array Empty | Medium | Greedy, Array, Hash Table +1 |
| 2981 | Find Longest Special Substring That Occurs Thrice I | Medium | Hash Table, String, Binary Search +2 |
| 2982 | Find Longest Special Substring That Occurs Thrice II | Medium | Hash Table, String, Binary Search +2 |
| 1198 | Find Smallest Common Element in All RowsPremium | Medium | Array, Hash Table, Binary Search +2 |
| 1612 | Check If Two Expression Trees are EquivalentPremium | Medium | Tree, Depth-First Search, Hash Table +2 |
| 1940 | Longest Common Subsequence Between Sorted ArraysPremium | Medium | Array, Hash Table, Counting |
| 2067 | Number of Equal Count SubstringsPremium | Medium | Hash Table, String, Counting +1 |
| 2083 | Substrings That Begin and End With the Same LetterPremium | Medium | Hash Table, Math, String +2 |
| 2168 | Unique Substrings With Equal Digit FrequencyPremium | Medium | Hash Table, String, Counting +2 |
| 2268 | Minimum Number of KeypressesPremium | Medium | Greedy, Hash Table, String +2 |
| 2539 | Count the Number of Good SubsequencesPremium | Medium | Hash Table, Math, String +2 |
| 2782 | Number of Unique CategoriesPremium | Medium | Union Find, Counting, Interactive |
| 2950 | Number of Divisible SubstringsPremium | Medium | Hash Table, String, Counting +1 |
| 2955 | Number of Same-End SubstringsPremium | Medium | Array, Hash Table, String +2 |
Hard (14)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 992 | Subarrays with K Different Integers | Hard | Array, Hash Table, Counting +1 |
| 1255 | Maximum Score Words Formed by Letters | Hard | Bit Manipulation, Array, Hash Table +5 |
| 1782 | Count Pairs Of Nodes | Hard | Graph, Array, Hash Table +4 |
| 1819 | Number of Different Subsequences GCDs | Hard | Array, Math, Counting +1 |
| 1857 | Largest Color Value in a Directed Graph | Hard | Graph, Topological Sort, Memoization +3 |
| 1994 | The Number of Good Subsets | Hard | Bit Manipulation, Array, Hash Table +5 |
| 2014 | Longest Subsequence Repeated k Times | Hard | Greedy, String, Backtracking +2 |
| 2025 | Maximum Number of Ways to Partition an Array | Hard | Array, Hash Table, Counting +2 |
| 2416 | Sum of Prefix Scores of Strings | Hard | Trie, Array, String +1 |
| 2499 | Minimum Total Cost to Make Arrays Unequal | Hard | Greedy, Array, Hash Table +1 |
| 2514 | Count Anagrams | Hard | Hash Table, Math, String +2 |
| 2547 | Minimum Cost to Split an Array | Hard | Array, Hash Table, Dynamic Programming +1 |
| 358 | Rearrange String k Distance ApartPremium | Hard | Greedy, Hash Table, String +3 |
| 1121 | Divide Array Into Increasing SequencesPremium | Hard | Array, Counting |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Design122
- Sliding Window116
- Backtracking105
When the Counting 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.