Basics for HashMap in Python
Get keys: use .keys(). You can iterate over this using a for loop.
keys = hash_map.keys()
for key in keys:
print(key)
values = hash_map.values()
for val in values:
print(val)
Example 1: 1. Two Sum
Given an array of integers nums and an integer target, return indices of two numbers such that they add up to target. You cannot use the same index twice.
Example 2: 2351. First Letter to Appear Twice
Given a string s, return the first character to appear twice. It is guaranteed that the input will have a duplicate character.
Example 3: Given an integer array nums, find all the numbers x in nums that satisfy the following: x + 1 is not in nums, and x - 1 is not in nums.
If a valid number x appears multiple times, you only need to include it in the answer once.
Check if the Sentence Is Pangram
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.
Missing Number
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Counting Elements
Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them separately.
Counting
Example 1: You are given a string s and an integer k. Find the length of the longest substring that contains at most k distinct characters.
For example, given s = “eceba” and k = 2, return 3. The longest substring with at most 2 distinct characters is “ece”.
Counting
Example 2: 2248. Intersection of Multiple Arrays
Given a 2D array nums that contains n arrays of distinct integers, return a sorted array containing all the numbers that appear in all n arrays.
For example, given nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]], return [3, 4]. 3 and 4 are the only numbers that are in all arrays.
Counting
Example 3: 1941. Check if All Characters Have Equal Number of Occurrences
Given a string s, determine if all characters have the same frequency.
For example, given s = “abacbc”, return true, because all characters appear twice. Given s = “aaabb”, return false. “a” appears 3 times, “b” appears 2 times. 3 != 2.
Counting
Given an integer array nums and an integer k, find the number of subarrays whose sum is equal to k.
Counting
Example 5: 1248. Count Number of Nice Subarrays
Given an array of positive integers nums and an integer k. Find the number of subarrays with exactly k odd numbers in them.
For example, given nums = [1, 1, 2, 1, 1], k = 3, the answer is 2. The subarrays with 3 odd numbers in them are [1, 1, 2, 1, 1] and [1, 1, 2, 1, 1].
Given an array of positive integers nums and an integer k. Find the number of subarrays with AT MOST k odd numbers in them.
Find Players With Zero or One Losses
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of size 2 where:
answer[0] is a list of all players that have not lost any matches.
answer[1] is a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
Note:
You should only consider the players that have played at least one match.
The testcases will be generated such that no two matches will have the same outcome.
Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.
Maximum Number of Balloons
Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible.
You can use each character in text at most once. Return the maximum number of instances that can be formed.
Contiguous Array
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Example 1: 49. Group Anagrams
Given an array of strings strs, group the anagrams together.
For example, given strs = [“eat”,”tea”,”tan”,”ate”,”nat”,”bat”], return [[“bat”],[“nat”,”tan”],[“ate”,”eat”,”tea”]].
Example 2: 2260. Minimum Consecutive Cards to Pick Up
Given an integer array cards, find the length of the shortest subarray that contains at least one duplicate. If the array has no duplicates, return -1.
Example 3: 2342. Max Sum of a Pair With Equal Sum of Digits
Given an array of integers nums, find the maximum value of nums[i] + nums[j], where nums[i] and nums[j] have the same digit sum (the sum of their individual digits). Return -1 if there is no pair of numbers with the same digit sum.
Example 4: 2352. Equal Row and Column Pairs
Given an n x n matrix grid, return the number of pairs (R, C) where R is a row and C is a column, and R and C are equal if we consider them as 1D arrays.
What are common gotchas with Counter (missing keys, negatives, cleaning), and quick patterns for equal frequencies / anagrams?
LC 1941 — Check if All Characters Have Equal Number of Occurrences
Ransom Note
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
Jewels and Stones
You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so “a” is considered a different type of stone from “A”.