s0
and s1
, return the number of substrings where s1
contains any anagram of s0
.
Constraints
n ≤ 100,000
where n
is the length of s0
m ≤ 100,000
where m
is the length of s1
https://binarysearch.com/problems/Anagram-Substrings
Input
abc
bcabxabc
Output
3
Explanation
The substrings "bca"
, "cab"
and "abc"
of s0
are permutations of "abc"
.
n
and a two-dimensional list of integers enemies
. Integer n
means there’s n
people labeled from [0, n - 1]
. Each element in enemies
contains [person_a, person_b]
which means that person_a
and person_b
are enemies.
Return whether it’s possible to partition the n
people into two groups such that no two people that are enemies are in the same group.
Constraints
n ≤ 1,000
m ≤ 10,000
where m
is the length of enemies
https://binarysearch.com/problems/Separate-People-Given-Dislike-Relations
Input
4
[[0,1],
[1,2]]
Output
True
Explanation
We can have these two groups [0, 2, 3]
and [1]
.
Input
3
[[0,1],
[0,2],
[1,2]]
Output
False
Explanation
No matter how we split the two groups, there will be two people that are enemies in a group.
weights
and values
which have the same length and an integer capacity
. weights[i]
and values[i]
represent the weight and value of the i
th item.
Given that you can take at most capacity
weights, and that you can take a fraction of an item’s weight with proportionate value, return the maximum amount of value you can get, rounded down to the nearest integer.
Constraints
n ≤ 100,000
where n
is the length of weights
and values
https://binarysearch.com/problems/Fractional-Knapsack
Input
[5, 6, 2]
[100, 100, 1]
8
Output
150
Explanation
The best we can do is:
5
weight and take the 100
value, leaving us with 3
capacity6
weight and take half of the 100
value.Input
[5, 6, 2]
[100, 100, 1]
13
Output
201
Explanation
We can take all 3
items in full
nums
and an integer diff
, return the length of the longest arithmetic subsequence where the difference between each consecutive numbers in the subsequence is diff
.
Constraints
0 ≤ n ≤ 100,000
where n
is the length of nums
https://binarysearch.com/problems/Longest-Arithmetic-Subsequence-with-Difference-Constraint
Input
[-2, 0, 3, 6, 1, 9]
3
Output
4
Explanation
We can pick the subsequence [0, 3, 6, 9]
.
Input
[9, 8, 7, 5, 3]
-2
Output
4
Explanation
We can pick the subsequence [9, 7, 5, 3]
.
matrix
and an integer k
, return the largest sum of a rectangle ≤ k
.
Constraints
n, m ≤ 100
where n
and m
are the number of rows and columns in matrix
.https://binarysearch.com/problems/Maximum-Sum-Rectangle-with-Condition
Input
[[ 2,-2],
[ 3, 2]]
6
Output
5
Explanation
We can take the rectangle [2, 3]
to get sum of 5
.
nums
, rearrange its order to form the largest possible integer and return it as a string.
Constraints
n ≤ 1,000
where n
is the length of nums
nums[i] ≤ 1,000
https://binarysearch.com/problems/Create-Largest-Number-From-a-List
Input
[10, 7, 76, 415]
Output
77641510
Input
[961, 745, 331, 794, 923]
Output
961923794745331
Input
[45, 14, 70, 67, 95]
Output
9570674514
Input
[70, 5, 94, 18, 78]
Output
947870518
nums
, remove numbers that appear multiple times in the list, while maintaining order of the appearance in the original list.
It should use \(\mathcal{O}(k)\) space where k
is the number of unique integers.
Constraints
n ≤ 100,000
where n
is the length of nums
https://binarysearch.com/problems/Remove-Duplicate-Numbers
Input
[1, 3, 5, 0, 3, 5, 8]
Output
[1, 0, 8]
Explanation
Only [1, 0, 8] are unique in the list and that’s the order they appear in.
matrix
, find the length of the longest strictly increasing path. You can move up, down, left, or right.
Constraints
n, m ≤ 500
where n
and m
are the number of rows and columns in matrix
https://binarysearch.com/problems/Longest-Increasing-Path
Input
[[1,3,5],
[0,4,6],
[2,2,9]]
Output
6
Explanation
The longest path is [0, 1, 3, 5, 6, 9]
k
and a list of integers nums
, return the length of the longest sublist that contains at most k
distinct integers.
Constraints
0 ≤ k ≤ n ≤ 100,000
where n
is the length of nums
https://binarysearch.com/problems/Longest-Sublist-with-K-Distinct-Numbers
Input
1
[0, 0, 0, 0, 0]
Output
5
Input
2
[0, 1, 2, 1, 0]
Output
3
Explanation
The longest substring with 2
distinct integers is [1,2,1]
, which has length of 3
.
Input
1
[0, 1, 2, 3, 4]
Output
1
a
and b
that are the same length. Partition them into substrings such that each pair of a
and b
’s substring is the same size and they are anagrams of each other. Return the cut indexes such that it results in the largest number of cuts of a
and b
. Return the empty list if no result exists.
Constraints
n ≤ 100,000
where n
is the length of a
and b
https://binarysearch.com/problems/Anagram-Partitioning
Input
study
dusty
Output
[0, 4]
Explanation
We can cut the string into 2 partitions:
a = ["stud", "y"]
b = ["dust", "y"]
Input
catdogwolf
actgodflow
Output
[0, 2, 3, 6]
Explanation
We can cut the string into 4 partitions such that each string is an anagram of each other.
a = ["ca", "t", "dog", "wolf"]
b = ["ac", "t", "god", "flow"]