š
LeetGoat
Interview Prep
Competitive Programming
Discord
String templates
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Pattern matching
longestPrefixMatches(text, pattern) O(n + m)
-> list of size len(text): arr[i] = length of match between pattern and text[i...] (Z-function)
multiPatternLongestPrefixMatches(text, patterns) O(n + m) expected
-> res of len(text): res[idx] = longest L where text[idx : idx + L] is a prefix of some pattern (suffix automaton)
multiPatternLongestPrefixInText(text, patterns) O(n + m) expected
-> res of len(patterns): res[i] = longest L where patterns[i][:L] appears somewhere in text (suffix automaton)
findMatches(text, pattern) O(n + m)
-> list of indices i where text[i...] matches pattern (Z-function)
multiPatternMatches(text, patterns) O(n + m + n ām) expected
-> res of len(patterns): res[i] = list of every idx where text[idx...] matches patterns[i] (Aho-Corasick)
multiPatternCounts(text, patterns) O(n + m) expected
-> res of len(patterns): res[i] = number of times patterns[i] occurs (Aho-Corasick)
Z-function
zFunction(arrOrString) O(n)
-> z list: z[i] = longest common prefix of the entire sequence against the suffix starting at i (z[0] = n)
KMP
properBorders(arrOrString) O(n)
-> properBorders[i] = longest proper prefix of arrOrString[0..i] that is also a suffix of it
Substrings
longestCommonSubstring(a, b) O(n + m) expected
-> (length, startInA, startInB) of the longest contiguous run in both (suffix automaton)
countDistinctSubstrings(arrOrString) O(n) expected
-> number of distinct non-empty contiguous substrings (suffix automaton)
distinctSubstringsByLength(arrOrString) O(n) expected
-> list of size n + 1: res[L] = number of distinct contiguous substrings of length L (suffix automaton)
Palindromes
longest palindromes around i and i+1 O(n)
returns two lists of tuples (l, r) for the longest palindrome centered around i, and i+1
Eertree (palindromic tree)
Updates
append(x)
add x at the end
appendLeft(x)
add x at the front
pop()
-> the last letter
removes the last letter from the eertree
popLeft()
-> the first letter
removes the first letter from the eertree
Ending at R
longestPalEndingAtCurrentR()
-> length
length of the longest palindrome ending at the current right end
numPalsEndingAtCurrentR()
-> count
number of palindromic substrings ending at the current right end
leftmostLForPalEndingAtR(r)
-> l
smallest l with seq[l..r] a palindrome
leftmostLForPalEndingAtEachR()
-> list of l, one per r
leftmostLForPalEndingAtR for every r
numDistinctPalsInPrefixEndingAtR(r)
-> count
distinct palindromic substrings of seq[0..r]
numDistinctPalsInPrefixEndingAtEachR()
-> list of counts, one per r
numDistinctPalsInPrefixEndingAtR for every r
numPalsEndingAtR(r)
-> count
palindromic substrings ending exactly at r
numPalsEndingAtEachR()
-> list of counts, one per r
numPalsEndingAtR for every r
Starting at L
longestPalStartingAtCurrentL()
-> length
length of the longest palindrome starting at the current left end
numPalsStartingAtCurrentL()
-> count
number of palindromic substrings starting at the current left end
rightmostRForPalStartingAtL(l)
-> r
largest r with seq[l..r] a palindrome
rightmostRForPalStartingAtEachL()
-> list of r, one per l
rightmostRForPalStartingAtL for every l
Whole sequence
numDistinctPals()
-> count
number of distinct palindromic substrings
numPalsTotal()
-> count
number of palindromic substrings, counting repeats ("aa" ā 3)
distinctPalsWithCounts()
-> list of (l, r, count)
per distinct palindrome: its first occurrence seq[l..r] and how often it occurs
longestPal()
-> length
length of the longest palindromic substring
minPalPartition()
-> count
fewest palindromes the sequence can be cut into
Template
Eertree
Open code ā
Eertree(arrOrString)
O(N) expected
memory
O(N)
Rotations
minRotation(arrOrString) O(n)
-> the lexicographically smallest rotation (same type as the input) (Duval)
maxRotation(arrOrString) O(n)
-> the lexicographically largest rotation (same type as the input) (Duval)
minRotationIndices(arrOrString) O(n)
-> every start index giving the smallest rotation (ascending) (Duval)
maxRotationIndices(arrOrString) O(n)
-> every start index giving the largest rotation (ascending) (Duval)
Misc.
shortestPeriod(arrOrString) O(n)
-> length of the shortest prefix that repeats a whole number of times to form the sequence: must perfectly divide (n if none) (KMP)
Overflow allowed