2023-12-29
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
You are given an integer array
Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Example 2:
Example 3:
Constraints:
•
•
•
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
d days. Jobs are dependent (i.e To work on the i^th job, you have to finish all the jobs j where 0 <= j < i).You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
d days. The difficulty of a day is the maximum difficulty of a job done on that day.You are given an integer array
jobDifficulty and an integer d. The difficulty of the i^th job is jobDifficulty[i].Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
Output: 7
Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7
Example 2:
Input: jobDifficulty = [9,9,9], d = 4
Output: -1
Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
Example 3:
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.
Constraints:
•
1 <= jobDifficulty.length <= 300•
0 <= jobDifficulty[i] <= 1000•
1 <= d <= 102023-12-30
1897. Redistribute Characters to Make All Strings Equal
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given an array of strings
In one operation, pick two distinct indices
Return
Example 1:
Example 2:
Constraints:
•
•
•
1897. Redistribute Characters to Make All Strings Equal
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given an array of strings
words (0-indexed).In one operation, pick two distinct indices
i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j].Return
true if you can make every string in words equal using any number of operations, and false otherwise.Example 1:
Input: words = ["abc","aabc","bc"]
Output: true
Explanation: Move the first 'a' in words[1] to the front of words[2],
to make words[1] = "abc" and words[2] = "abc".
All the strings are now equal to "abc", so return true.
Example 2:
Input: words = ["ab","a"]
Output: false
Explanation: It is impossible to make all the strings equal using the operation.
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 100•
words[i] consists of lowercase English letters.2023-12-31
1624. Largest Substring Between Two Equal Characters
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a string
A substring is a contiguous sequence of characters within a string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1624. Largest Substring Between Two Equal Characters
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a string
s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "aa"
Output: 0
Explanation: The optimal substring here is an empty substring between the two 'a's.
Example 2:
Input: s = "abca"
Output: 2
Explanation: The optimal substring here is "bc".
Example 3:
Input: s = "cbzxy"
Output: -1
Explanation: There are no characters that appear twice in s.
Constraints:
•
1 <= s.length <= 300•
s contains only lowercase English letters.2024-01-01
455. Assign Cookies
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Easy
Problem:
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child
Example 1:
Example 2:
Constraints:
•
•
•
455. Assign Cookies
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Easy
Problem:
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child
i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.Example 1:
Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.
Example 2:
Input: g = [1,2], s = [1,2,3]
Output: 2
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
Constraints:
•
1 <= g.length <= 3 * 10^4•
0 <= s.length <= 3 * 10^4•
1 <= g[i], s[j] <= 2^31 - 12024-01-02
2610. Convert an Array Into a 2D Array With Conditions
Topic: Array, Hash Table
Difficulty: Medium
Problem:
You are given an integer array
• The 2D array should contain only the elements of the array
• Each row in the 2D array contains distinct integers.
• The number of rows in the 2D array should be minimal.
Return the resulting array. If there are multiple answers, return any of them.
Note that the 2D array can have a different number of elements on each row.
Example 1:
Example 2:
Constraints:
•
•
2610. Convert an Array Into a 2D Array With Conditions
Topic: Array, Hash Table
Difficulty: Medium
Problem:
You are given an integer array
nums. You need to create a 2D array from nums satisfying the following conditions:• The 2D array should contain only the elements of the array
nums.• Each row in the 2D array contains distinct integers.
• The number of rows in the 2D array should be minimal.
Return the resulting array. If there are multiple answers, return any of them.
Note that the 2D array can have a different number of elements on each row.
Example 1:
Input: nums = [1,3,4,1,2,3,1]
Output: [[1,3,4,2],[1,3],[1]]
Explanation: We can create a 2D array that contains the following rows:
- 1,3,4,2
- 1,3
- 1
All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
It can be shown that we cannot have less than 3 rows in a valid array.
Example 2:
Input: nums = [1,2,3,4]
Output: [[4,3,2,1]]
Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
Constraints:
•
1 <= nums.length <= 200•
1 <= nums[i] <= nums.length2024-01-03
2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
• For each row
Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Constraints:
•
•
•
•
2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the i^th row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
r_1 and r_2, where r_1 < r_2.• For each row
i where r_1 < i < r_2, there are no security devices in the i^th row.Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Input: bank = ["011001","000000","010100","001000"]
Output: 8
Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
* bank[0][1] -- bank[2][1]
* bank[0][1] -- bank[2][3]
* bank[0][2] -- bank[2][1]
* bank[0][2] -- bank[2][3]
* bank[0][5] -- bank[2][1]
* bank[0][5] -- bank[2][3]
* bank[2][1] -- bank[3][2]
* bank[2][3] -- bank[3][2]
Note that there is no beam between any device on the 0^th row with any on the 3^rd row.
This is because the 2^nd row contains security devices, which breaks the second condition.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Input: bank = ["000","111","000"]
Output: 0
Explanation: There does not exist two devices located on two different rows.
Constraints:
•
m == bank.length•
n == bank[i].length•
1 <= m, n <= 500•
bank[i][j] is either '0' or '1'.2024-01-04
2870. Minimum Number of Operations to Make Array Empty
Topic: Array, Hash Table, Greedy, Counting
Difficulty: Medium
Problem:
You are given a 0-indexed array
There are two types of operations that you can apply on the array any number of times:
• Choose two elements with equal values and delete them from the array.
• Choose three elements with equal values and delete them from the array.
Return the minimum number of operations required to make the array empty, or
Example 1:
Example 2:
Constraints:
•
•
2870. Minimum Number of Operations to Make Array Empty
Topic: Array, Hash Table, Greedy, Counting
Difficulty: Medium
Problem:
You are given a 0-indexed array
nums consisting of positive integers.There are two types of operations that you can apply on the array any number of times:
• Choose two elements with equal values and delete them from the array.
• Choose three elements with equal values and delete them from the array.
Return the minimum number of operations required to make the array empty, or
-1 if it is not possible.Example 1:
Input: nums = [2,3,3,2,2,4,2,3,4]
Output: 4
Explanation: We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.
Example 2:
Input: nums = [2,1,2,2,3,3]
Output: -1
Explanation: It is impossible to empty the array.
Constraints:
•
2 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62024-01-05
300. Longest Increasing Subsequence
Topic: Array, Binary Search, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
Follow up: Can you come up with an algorithm that runs in
300. Longest Increasing Subsequence
Topic: Array, Binary Search, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
nums, return the length of the longest strictly increasing subsequence.Example 1:
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:
Input: nums = [0,1,0,3,2,3]
Output: 4
Example 3:
Input: nums = [7,7,7,7,7,7,7]
Output: 1
Constraints:
•
1 <= nums.length <= 2500•
-10^4 <= nums[i] <= 10^4Follow up: Can you come up with an algorithm that runs in
O(n log(n)) time complexity?2024-01-06
1235. Maximum Profit in Job Scheduling
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
We have
You're given the
If you choose a job that ends at time
Example 1:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png
Example 3:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png
Constraints:
•
•
•
1235. Maximum Profit in Job Scheduling
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
We have
n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].You're given the
startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.If you choose a job that ends at time
X you will be able to start another job that starts at time X.Example 1:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
Output: 120
Explanation: The subset chosen is the first and fourth job.
Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
Example 2:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
Output: 150
Explanation: The subset chosen is the first, fourth and fifth job.
Profit obtained 150 = 20 + 70 + 60.
Example 3:
Image: https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
Output: 6
Constraints:
•
1 <= startTime.length == endTime.length == profit.length <= 5 * 10^4•
1 <= startTime[i] < endTime[i] <= 10^9•
1 <= profit[i] <= 10^42024-01-07
446. Arithmetic Slices II - Subsequence
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
Given an integer array
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
• For example,
• For example,
A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
• For example,
The test cases are generated so that the answer fits in 32-bit integer.
Example 1:
Example 2:
Constraints:
•
•
446. Arithmetic Slices II - Subsequence
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
Given an integer array
nums, return the number of all the arithmetic subsequences of nums.A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
• For example,
[1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences.• For example,
[1, 1, 2, 5, 7] is not an arithmetic sequence.A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
• For example,
[2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].The test cases are generated so that the answer fits in 32-bit integer.
Example 1:
Input: nums = [2,4,6,8,10]
Output: 7
Explanation: All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
Example 2:
Input: nums = [7,7,7,7,7]
Output: 16
Explanation: Any subsequence of this array is arithmetic.
Constraints:
•
1 <= nums.length <= 1000•
-2^31 <= nums[i] <= 2^31 - 12024-01-08
938. Range Sum of BST
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
•
• All
938. Range Sum of BST
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
Constraints:
• The number of nodes in the tree is in the range
[1, 2 * 10^4].•
1 <= Node.val <= 10^5•
1 <= low <= high <= 10^5• All
Node.val are unique.2024-01-09
872. Leaf-Similar Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png
For example, in the given tree above, the leaf value sequence is
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg
Constraints:
• The number of nodes in each tree will be in the range
• Both of the given trees will have values in the range
872. Leaf-Similar Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png
For example, in the given tree above, the leaf value sequence is
(6, 7, 4, 9, 8).Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return
true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg
Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg
Input: root1 = [1,2,3], root2 = [1,3,2]
Output: false
Constraints:
• The number of nodes in each tree will be in the range
[1, 200].• Both of the given trees will have values in the range
[0, 200].2024-01-10
2385. Amount of Time for Binary Tree to Be Infected
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
Each minute, a node becomes infected if:
• The node is currently uninfected.
• The node is adjacent to an infected node.
Return the number of minutes needed for the entire tree to be infected.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png
Constraints:
• The number of nodes in the tree is in the range
•
• Each node has a unique value.
• A node with a value of
2385. Amount of Time for Binary Tree to Be Infected
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree with unique values, and an integer start. At minute 0, an infection starts from the node with value start.Each minute, a node becomes infected if:
• The node is currently uninfected.
• The node is adjacent to an infected node.
Return the number of minutes needed for the entire tree to be infected.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png
Input: root = [1,5,3,null,4,10,6,9,2], start = 3
Output: 4
Explanation: The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png
Input: root = [1], start = 1
Output: 0
Explanation: At minute 0, the only node in the tree is infected so we return 0.
Constraints:
• The number of nodes in the tree is in the range
[1, 10^5].•
1 <= Node.val <= 10^5• Each node has a unique value.
• A node with a value of
start exists in the tree.2024-01-11
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
A node
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Constraints:
• The number of nodes in the tree is in the range
•
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.A node
a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Input: root = [1,null,2,null,0,3]
Output: 3
Constraints:
• The number of nodes in the tree is in the range
[2, 5000].•
0 <= Node.val <= 10^52024-01-12
1704. Determine if String Halves Are Alike
Topic: String, Counting
Difficulty: Easy
Problem:
You are given a string
Two strings are alike if they have the same number of vowels (
Return
Example 1:
Example 2:
Constraints:
•
•
•
1704. Determine if String Halves Are Alike
Topic: String, Counting
Difficulty: Easy
Problem:
You are given a string
s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.Two strings are alike if they have the same number of vowels (
'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.Return
true if a and b are alike. Otherwise, return false.Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Constraints:
•
2 <= s.length <= 1000•
s.length is even.•
s consists of uppercase and lowercase letters.2024-01-13
1347. Minimum Number of Steps to Make Two Strings Anagram
Topic: Hash Table, String, Counting
Difficulty: Medium
Problem:
You are given two strings of the same length
Return the minimum number of steps to make
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1347. Minimum Number of Steps to Make Two Strings Anagram
Topic: Hash Table, String, Counting
Difficulty: Medium
Problem:
You are given two strings of the same length
s and t. In one step you can choose any character of t and replace it with another character.Return the minimum number of steps to make
t an anagram of s.An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example 1:
Input: s = "bab", t = "aba"
Output: 1
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
Example 2:
Input: s = "leetcode", t = "practice"
Output: 5
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
Example 3:
Input: s = "anagram", t = "mangaar"
Output: 0
Explanation: "anagram" and "mangaar" are anagrams.
Constraints:
•
1 <= s.length <= 5 * 10^4•
s.length == t.length•
s and t consist of lowercase English letters only.2024-01-14
1657. Determine if Two Strings Are Close
Topic: Hash Table, String, Sorting, Counting
Difficulty: Medium
Problem:
Two strings are considered close if you can attain one from the other using the following operations:
• Operation 1: Swap any two existing characters.
• For example,
• Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
• For example,
You can use the operations on either string as many times as necessary.
Given two strings,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1657. Determine if Two Strings Are Close
Topic: Hash Table, String, Sorting, Counting
Difficulty: Medium
Problem:
Two strings are considered close if you can attain one from the other using the following operations:
• Operation 1: Swap any two existing characters.
• For example,
abcde -> aecdb• Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
• For example,
aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)You can use the operations on either string as many times as necessary.
Given two strings,
word1 and word2, return true if word1 and word2 are close, and false otherwise.Example 1:
Input: word1 = "abc", word2 = "bca"
Output: true
Explanation: You can attain word2 from word1 in 2 operations.
Apply Operation 1: "abc" -> "acb"
Apply Operation 1: "acb" -> "bca"
Example 2:
Input: word1 = "a", word2 = "aa"
Output: false
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
Example 3:
Input: word1 = "cabbba", word2 = "abbccc"
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -> "caabbb"
Apply Operation 2: "caabbb" -> "baaccc"
Apply Operation 2: "baaccc" -> "abbccc"
Constraints:
•
1 <= word1.length, word2.length <= 10^5•
word1 and word2 contain only lowercase English letters.2024-01-15
2225. Find Players With Zero or One Losses
Topic: Array, Hash Table, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
Return a list
•
•
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.
Example 1:
Example 2:
Constraints:
•
•
•
•
• All
2225. Find Players With Zero or One Losses
Topic: Array, Hash Table, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
matches where matches[i] = [winner_i, loser_i] indicates that the player winner_i defeated player loser_i 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.
Example 1:
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
Example 2:
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
Output: [[1,2,5,6],[]]
Explanation:
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
Constraints:
•
1 <= matches.length <= 10^5•
matches[i].length == 2•
1 <= winner_i, loser_i <= 10^5•
winner_i != loser_i• All
matches[i] are unique.2024-01-16
380. Insert Delete GetRandom O(1)
Topic: Array, Hash Table, Math, Design, Randomized
Difficulty: Medium
Problem:
Implement the
•
•
•
•
You must implement the functions of the class such that each function works in average
Example 1:
Constraints:
•
• At most
• There will be at least one element in the data structure when
380. Insert Delete GetRandom O(1)
Topic: Array, Hash Table, Math, Design, Randomized
Difficulty: Medium
Problem:
Implement the
RandomizedSet class:•
RandomizedSet() Initializes the RandomizedSet object.•
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.•
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.•
int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.You must implement the functions of the class such that each function works in average
O(1) time complexity.Example 1:
Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]
Explanation
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
Constraints:
•
-2^31 <= val <= 2^31 - 1• At most
2 *10^5 calls will be made to insert, remove, and getRandom.• There will be at least one element in the data structure when
getRandom is called.2024-01-17
1207. Unique Number of Occurrences
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given an array of integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1207. Unique Number of Occurrences
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given an array of integers
arr, return true if the number of occurrences of each value in the array is unique or false otherwise.Example 1:
Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
Example 2:
Input: arr = [1,2]
Output: false
Example 3:
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true
Constraints:
•
1 <= arr.length <= 1000•
-1000 <= arr[i] <= 1000