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)

#ProblemDifficultyTopics
169Majority ElementEasyArray, Hash Table, Divide and Conquer +2
383Ransom NoteEasyHash Table, String, Counting
387First Unique Character in a StringEasyQueue, Hash Table, String +1
594Longest Harmonious SubsequenceEasyArray, Hash Table, Counting +2
819Most Common WordEasyArray, Hash Table, String +1
884Uncommon Words from Two SentencesEasyHash Table, String, Counting
914X of a Kind in a Deck of CardsEasyArray, Hash Table, Math +2
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
1221Split a String in Balanced StringsEasyGreedy, String, Counting
1356Sort Integers by The Number of 1 BitsEasyBit Manipulation, Array, Counting +1
1370Increasing Decreasing StringEasyHash Table, String, Counting
1394Find Lucky Integer in an ArrayEasyArray, Hash Table, Counting
1512Number of Good PairsEasyArray, Hash Table, Math +1
1603Design Parking SystemEasyDesign, Counting, Simulation
1684Count the Number of Consistent StringsEasyBit Manipulation, Array, Hash Table +2
1704Determine if String Halves Are AlikeEasyString, Counting
1742Maximum Number of Balls in a BoxEasyHash Table, Math, Counting
1748Sum of Unique ElementsEasyArray, Hash Table, Counting
1790Check if One String Swap Can Make Strings EqualEasyHash Table, String, Counting
1854Maximum Population YearEasyArray, Counting, Prefix Sum
1876Substrings of Size Three with Distinct CharactersEasyHash Table, String, Counting +1
1897Redistribute Characters to Make All Strings EqualEasyHash Table, String, Counting
1941Check if All Characters Have Equal Number of OccurrencesEasyHash Table, String, Counting
2006Count Number of Pairs With Absolute Difference KEasyArray, Hash Table, Counting
2053Kth Distinct String in an ArrayEasyArray, Hash Table, String +1
2068Check Whether Two Strings are Almost EquivalentEasyHash Table, String, Counting
2085Count Common Words With One OccurrenceEasyArray, Hash Table, String +1
2148Count Elements With Strictly Smaller and Greater ElementsEasyArray, Counting, Sorting
2190Most Frequent Number Following Key In an ArrayEasyArray, Hash Table, Counting
2206Divide Array Into Equal PairsEasyBit Manipulation, Array, Hash Table +1
2248Intersection of Multiple ArraysEasyArray, Hash Table, Counting +1
2283Check if Number Has Equal Digit Count and Digit ValueEasyHash Table, String, Counting
2287Rearrange Characters to Make Target StringEasyHash Table, String, Counting
2341Maximum Number of Pairs in ArrayEasyArray, Hash Table, Counting
2347Best Poker HandEasyArray, Hash Table, Counting
2351First Letter to Appear TwiceEasyBit Manipulation, Hash Table, String +1
2404Most Frequent Even ElementEasyArray, Hash Table, Counting
2423Remove Letter To Equalize FrequencyEasyHash Table, String, Counting
2506Count Pairs Of Similar StringsEasyBit Manipulation, Array, Hash Table +2
2529Maximum Count of Positive Integer and Negative IntegerEasyArray, Binary Search, Counting
2586Count the Number of Vowel Strings in RangeEasyArray, String, Counting
2748Number of Beautiful PairsEasyArray, Hash Table, Math +2
2833Furthest Point From OriginEasyString, Counting
2960Count Tested Devices After Test OperationsEasyArray, Counting, Simulation
1213Intersection of Three Sorted ArraysPremiumEasyArray, Hash Table, Binary Search +1

Medium (65)

#ProblemDifficultyTopics
229Majority Element IIMediumArray, Hash Table, Counting +1
299Bulls and CowsMediumHash Table, String, Counting
347Top K Frequent ElementsMediumArray, Hash Table, Divide and Conquer +5
451Sort Characters By FrequencyMediumHash Table, String, Bucket Sort +3
621Task SchedulerMediumGreedy, Array, Hash Table +3
692Top K Frequent WordsMediumTrie, Array, Hash Table +5
767Reorganize StringMediumGreedy, Hash Table, String +3
811Subdomain Visit CountMediumArray, Hash Table, String +1
869Reordered Power of 2MediumHash Table, Math, Counting +2
900RLE IteratorMediumDesign, Array, Counting +1
9233Sum With MultiplicityMediumArray, Hash Table, Two Pointers +2
945Minimum Increment to Make Array UniqueMediumGreedy, Array, Counting +1
1010Pairs of Songs With Total Durations Divisible by 60MediumArray, Hash Table, Counting
1054Distant BarcodesMediumGreedy, Array, Hash Table +3
1079Letter Tile PossibilitiesMediumHash Table, String, Backtracking +1
1090Largest Values From LabelsMediumGreedy, Array, Hash Table +2
1267Count Servers that CommunicateMediumDepth-First Search, Breadth-First Search, Union Find +3
1347Minimum Number of Steps to Make Two Strings AnagramMediumHash Table, String, Counting
1366Rank Teams by VotesMediumArray, Hash Table, String +2
1400Construct K Palindrome StringsMediumGreedy, Hash Table, String +1
1419Minimum Number of Frogs CroakingMediumString, Counting
1481Least Number of Unique Integers after K RemovalsMediumGreedy, Array, Hash Table +2
1497Check If Array Pairs Are Divisible by kMediumArray, Hash Table, Counting
1519Number of Nodes in the Sub-Tree With the Same LabelMediumTree, Depth-First Search, Breadth-First Search +2
1657Determine if Two Strings Are CloseMediumHash Table, String, Counting +1
1726Tuple with Same ProductMediumArray, Hash Table, Counting
1737Change Minimum Characters to Satisfy One of Three ConditionsMediumHash Table, String, Counting +1
1775Equal Sum Arrays With Minimum Number of OperationsMediumGreedy, Array, Hash Table +1
1781Sum of Beauty of All SubstringsMediumHash Table, String, Counting
1814Count Nice Pairs in an ArrayMediumArray, Hash Table, Math +1
2001Number of Pairs of Interchangeable RectanglesMediumArray, Hash Table, Math +2
2013Detect SquaresMediumDesign, Array, Hash Table +1
2023Number of Pairs of Strings With Concatenation Equal to TargetMediumArray, Hash Table, String +1
2029Stone Game IXMediumGreedy, Array, Math +2
2048Next Greater Numerically Balanced NumberMediumHash Table, Math, Backtracking +2
2131Longest Palindrome by Concatenating Two Letter WordsMediumGreedy, Array, Hash Table +2
2150Find All Lonely Numbers in the ArrayMediumArray, Hash Table, Counting
2170Minimum Operations to Make the Array AlternatingMediumGreedy, Array, Hash Table +1
2182Construct String With Repeat LimitMediumGreedy, Hash Table, String +2
2186Minimum Number of Steps to Make Two Strings Anagram IIMediumHash Table, String, Counting
2225Find Players With Zero or One LossesMediumArray, Hash Table, Counting +1
2244Minimum Rounds to Complete All TasksMediumGreedy, Array, Hash Table +1
2275Largest Combination With Bitwise AND Greater Than ZeroMediumBit Manipulation, Array, Hash Table +1
2284Sender With Largest Word CountMediumArray, Hash Table, String +1
2364Count Number of Bad PairsMediumArray, Hash Table, Math +1
2384Largest Palindromic NumberMediumGreedy, Hash Table, String +1
2442Count Number of Distinct Integers After Reverse OperationsMediumArray, Hash Table, Math +1
2453Destroy Sequential TargetsMediumArray, Hash Table, Counting
2526Find Consecutive Integers from a Data StreamMediumDesign, Queue, Hash Table +2
2531Make Number of Distinct Characters EqualMediumHash Table, String, Counting
2856Minimum Array Length After Pair RemovalsMediumGreedy, Array, Hash Table +3
2870Minimum Number of Operations to Make Array EmptyMediumGreedy, Array, Hash Table +1
2981Find Longest Special Substring That Occurs Thrice IMediumHash Table, String, Binary Search +2
2982Find Longest Special Substring That Occurs Thrice IIMediumHash Table, String, Binary Search +2
1198Find Smallest Common Element in All RowsPremiumMediumArray, Hash Table, Binary Search +2
1612Check If Two Expression Trees are EquivalentPremiumMediumTree, Depth-First Search, Hash Table +2
1940Longest Common Subsequence Between Sorted ArraysPremiumMediumArray, Hash Table, Counting
2067Number of Equal Count SubstringsPremiumMediumHash Table, String, Counting +1
2083Substrings That Begin and End With the Same LetterPremiumMediumHash Table, Math, String +2
2168Unique Substrings With Equal Digit FrequencyPremiumMediumHash Table, String, Counting +2
2268Minimum Number of KeypressesPremiumMediumGreedy, Hash Table, String +2
2539Count the Number of Good SubsequencesPremiumMediumHash Table, Math, String +2
2782Number of Unique CategoriesPremiumMediumUnion Find, Counting, Interactive
2950Number of Divisible SubstringsPremiumMediumHash Table, String, Counting +1
2955Number of Same-End SubstringsPremiumMediumArray, Hash Table, String +2

Hard (14)

#ProblemDifficultyTopics
992Subarrays with K Different IntegersHardArray, Hash Table, Counting +1
1255Maximum Score Words Formed by LettersHardBit Manipulation, Array, Hash Table +5
1782Count Pairs Of NodesHardGraph, Array, Hash Table +4
1819Number of Different Subsequences GCDsHardArray, Math, Counting +1
1857Largest Color Value in a Directed GraphHardGraph, Topological Sort, Memoization +3
1994The Number of Good SubsetsHardBit Manipulation, Array, Hash Table +5
2014Longest Subsequence Repeated k TimesHardGreedy, String, Backtracking +2
2025Maximum Number of Ways to Partition an ArrayHardArray, Hash Table, Counting +2
2416Sum of Prefix Scores of StringsHardTrie, Array, String +1
2499Minimum Total Cost to Make Arrays UnequalHardGreedy, Array, Hash Table +1
2514Count AnagramsHardHash Table, Math, String +2
2547Minimum Cost to Split an ArrayHardArray, Hash Table, Dynamic Programming +1
358Rearrange String k Distance ApartPremiumHardGreedy, Hash Table, String +3
1121Divide Array Into Increasing SequencesPremiumHardArray, Counting

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.