Hash Map Flashcards

(25 cards)

1
Q

Basics for HashMap in Python

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Counting

Given an integer array nums and an integer k, find the number of subarrays whose sum is equal to k.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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].

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given an array of positive integers nums and an integer k. Find the number of subarrays with AT MOST k odd numbers in them.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

17
Q

Contiguous Array

Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.

18
Q

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”]].

19
Q

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.

20
Q

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.

21
Q

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.

22
Q

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

23
Q

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.

24
Q

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”.

25
Longest Substring Without Repeating Characters Given a string s, find the length of the longest substring without duplicate characters.