Bit-Manipulation
Bit Manipulation
·5 mins
Algorithm
Bit-Manipulation
Common bit operations, AND only removes bits while OR only adds them, finding adjacent ones with n & (n » 1), and all-ones numbers 2^k - 1
Trie
·3 mins
Algorithm
Trie
String
Prefix
Bit-Manipulation
Building a Trie from nested dicts, counting on nodes, pairing it with DFS, and when a Trie is not actually needed
LeetCode 864: Shortest Path to Get All Keys
·3 mins
LeetCode
Hard
Bfs
Bit-Manipulation
Matrix
LeetCode 864 note: the BFS state is (position, bitmask of keys), so the same cell with different keys is a different node
LeetCode 1178: Number of Valid Words for Each Puzzle
·3 mins
LeetCode
Hard
Bit-Manipulation
Hash-Map
Trie
LeetCode 1178 note: a word only matters by its set of letters, so count 26-bit masks and enumerate each puzzle’s submasks
LeetCode 3950: Exactly One Consecutive Set Bits Pair
·1 min
LeetCode
Easy
Bit-Manipulation
LeetCode 3950 note: each 1 in n & (n » 1) marks a pair of adjacent ones, so exactly one pair means it has a single 1
LeetCode 3910: Count Connected Subgraphs with Even Node Sum
·3 mins
LeetCode
Hard
Bit-Manipulation
Enumeration
Graph
Union-Find
LeetCode 3910 note: n is tiny, so enumerate subsets with a bitmask, check the even sum first, then check that the induced subgraph is connected
LeetCode 3827: Count Monobit Integers
·1 min
LeetCode
Easy
Bit-Manipulation
Enumeration
LeetCode 3827 note: the only numbers with all-equal bits are 0 and 2^k - 1, so the count is (n + 1).bit_length()
LeetCode 2401: Longest Nice Subarray
·2 mins
LeetCode
Medium
Sliding-Window
Bit-Manipulation
Array
LeetCode 2401 note: pairwise AND 0 ⇔ each bit is used by at most one number; a mask holds the window’s bits, and on a conflict the left shrinks and its bits are removed
LeetCode 1763: Longest Nice Substring
·2 mins
LeetCode
Easy
Sliding-Window
Divide-and-Conquer
Hash-Table
String
Bit-Manipulation
LeetCode 1763 note: a character whose other case never appears cannot be in the answer, so split on it and recurse, preferring the left side on ties
LeetCode 1461: Check If a String Contains All Binary Codes of Size K
·2 mins
LeetCode
Medium
Hash-Table
String
Bit-Manipulation
Rolling-Hash
LeetCode 1461 note: put every length-k substring in a set and check that its size reaches 2^k; the last start is len(s) - k
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode
Medium
Sliding-Window
String
Bit-Manipulation
Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 1486: XOR Operation in an Array
·1 min
LeetCode
Easy
Math
Bit-Manipulation
LeetCode 1486 note: generate start + 2i by definition and XOR them together