Array & hasing

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/8

flashcard set

Earn XP

Description and Tags

Last updated 8:13 AM on 8/20/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

9 Terms

1
New cards

Two Sum - Easy

Given an array of integers nums and an integer target, return the indices i and j such that nums[i] + nums[j] == target and i != j.

You may assume that every input has exactly one pair of indices i and j that satisfy the condition.

Return the answer with the smaller index first.


Example 1:

Input: nums = [3,4,5,6], target = 7
Output: [0,1]


Example 2:

Input: nums = [4,5,6], target = 10
Output: [0,2]


For each number, look up its difference (target − num) in the map; if missing, store the number→index and move on.


Approach: Hash Map


Time: O(n)

Space: O(n)

<p>For each number, look up its difference (target − num) in the map; if missing, store the number→index and move on.</p><p></p><p>Approach: <strong>Hash Map</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(n)</strong></p>
2
New cards

Contain Duplicate - Easy

Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.


Example 1:

Input: nums = [1, 2, 3, 3]
Output: true


Example 2:

Input: nums = [1, 2, 3, 4]
Output: false



Add numbers to a set one by one; if one's already there, it's a duplicate.


Approach: Hash Set


Time: O(n)

Space: O(n)

<p>Add numbers to a set one by one; if one's already there, it's a duplicate.</p><p></p><p>Approach: <strong>Hash Set</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(n)</strong></p>
3
New cards

Valid Anagram - Easy

Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.

Two strings are anagrams if they contain the same characters, with each character appearing the same number of times, regardless of order.


Example 1:

Input: s = "racecar", t = "carrace"
Output: true


Example 2:

Input: s = "jar", t = "jam"
Output: false


Example 3:

Input: s = "x", t = "x"
Output: true


Count characters in both strings; they're anagrams if the two count maps match


Approach: Hash Map


Time: O(n)

Space: O(1) at most 26 lowercase letters

<p>Count characters in both strings; they're anagrams if the two count maps match</p><p></p><p>Approach: <strong>Hash Map</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(1)</strong> <em>at most 26 lowercase letters</em></p>
4
New cards

Group Anagram - Medium

Given an array of strings strs, group all anagrams together into sublists. You may return the output in any order.

An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.


Example 1:

Input: strs = ["act","pots","tops","cat","stop","hat"]
Output: [["hat"],["act", "cat"],["stop", "pots", "tops"]]


Example 2:

Input: strs = ["x"]
Output: [["x"]]


Bucket words in a map keyed by their sorted letter (or 26-letter count)


Approach: Hash Table for Character Count


Time: O(m * n)

Space: O(m * n)

Where m is the number of strings and n is the length of the longest string

<p>Bucket words in a map keyed by their sorted letter (or 26-letter count)</p><p></p><p>Approach: <strong>Hash Table for Character Count</strong></p><p></p><p>Time: <strong>O(m * n)</strong></p><p>Space: <strong>O(m * n)</strong></p><p><em>Where m is the number of strings and n is the length of the longest string</em></p>
5
New cards

Top K Frequent Elements - Medium

Given an integer array nums and an integer k, return the k most frequent elements within the array.

The test cases are generated such that the answer is always unique.

You may return the output in any order.


Example 1:

Input: nums = [1,2,2,3,3,3], k = 2
Output: [2,3]


Example 2:

Input: nums = [7,7], k = 1
Output: [7]


Count frequencies, then bucket sort by frequency and take the top k


Approach: Bucket Sort


Time: O(n)

Space: O(n)

<p>Count frequencies, then bucket sort by frequency and take the top k</p><p></p><p>Approach: <strong>Bucket Sort</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(n)</strong></p>
6
New cards

Encode and Decode Strings - Medium

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.

Prefix each string with its "length#", then decode by reading the length before each string


Approach: length#string per entry


Time: O(m+n) for each encode() and decode() function calls.

Space: O(m+n) for each encode() and decode() function calls.

(m = total length of all strings, n = number of strings)

<p>Prefix each string with its "length#", then decode by reading the length before each string</p><p></p><p>Approach: l<strong>ength#string per entry</strong></p><p></p><p>Time: <span style="color: inherit !important;"><strong>O(m+n)</strong><em> </em></span>for each encode() and decode() function calls.</p><p>Space: <span style="color: inherit !important;"><strong>O(m+n)</strong></span><span style="font-family: KaTeX_Main, &quot;Times New Roman&quot;, serif; line-height: 1.75; font-size: 18px; color: inherit !important;"><em> </em></span>for each encode() and decode() function calls.</p><p><em>(m = total length of all strings, n = number of strings)</em></p>
7
New cards

Product of Array Except Self - Medium

Given an integer array nums, return an array output where output[i] is the product of all the elements of nums except nums[i].

Each product is guaranteed to fit in a 32-bit integer.

Follow-up: Could you solve it in O(n)O(n) time without using the division operation?


Example 1:

Input: nums = [1,2,4,6]
Output: [48,24,12,8]


Example 2:

Input: nums = [-1,0,1,2,3]
Output: [0,-6,0,0,0]


One left to right prefix product pass, then one right to left suffix project pass


Approach: Prefix & Suffix (optimal)


Time: O(n)

Space: O(n) space for the output array, O(1) extra space.

<p>One left to right prefix product pass, then one right to left suffix project pass</p><p></p><p>Approach: <strong>Prefix &amp; Suffix (optimal)</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(n) </strong>space for the output array, O(1) extra space.</p>
8
New cards

Valid Sudoku - Medium

Given a 9×9 Sudoku board (partially filled, empty cells are “.”), determine if the current state is valid. A board is valid if each row, each column, and each 3×3 sub-box contains the digits 1–9 at most once. You only validate the current state, not solvability.

One pass tracking seen values in row, column, and box sets (with box key - row//3, col//3)


Approach: Hash Set (One pass)


Time: O(n2)

Space: O(n2)

<p>One pass tracking seen values in row, column, and box sets (with box key - row//3, col//3)</p><p></p><p>Approach: <strong>Hash Set (One pass)</strong></p><p></p><p>Time: <strong>O(n<sup>2</sup>)</strong></p><p>Space: <strong>O(n<sup>2</sup>)</strong></p>
9
New cards

Longest Consecutive Sequence - Medium

Given an array of integers nums, return the length of the longest consecutive sequence of elements that can be formed.

A consecutive sequence is a sequence of elements in which each element is exactly 1 greater than the previous element. The elements do not have to be consecutive in the original array.

You must write an algorithm that runs in O(n) time.


Example 1:

Input: nums = [2,20,4,10,3,4,5]
Output: 4


Example 2:

Input: nums = [0,3,2,5,4,6,1,1]
Output: 7


Put all numbers in a set; start counting a streak only from numbers whose predecessor is missing


Approach: Hash Set


Time: O(n)

Space: O(n)

<p>Put all numbers in a set; start counting a streak only from numbers whose predecessor is missing</p><p></p><p>Approach: <strong>Hash Set</strong></p><p></p><p>Time: <strong>O(n)</strong></p><p>Space: <strong>O(n)</strong></p>