2024-09-07
1367. Linked List in Binary Tree
Topic: Linked List, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
Return True if all the elements in the linked list starting from the
In this context downward path means a path that starts at some node and goes downwards.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png
Example 3:
Constraints:
• The number of nodes in the tree will be in the range
• The number of nodes in the list will be in the range
•
1367. Linked List in Binary Tree
Topic: Linked List, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the
head correspond to some downward path connected in the binary tree otherwise return False.In this context downward path means a path that starts at some node and goes downwards.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png
Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Explanation: Nodes in blue form a subpath in the binary Tree.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png
Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Example 3:
Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: false
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.
Constraints:
• The number of nodes in the tree will be in the range
[1, 2500].• The number of nodes in the list will be in the range
[1, 100].•
1 <= Node.val <= 100 for each node in the linked list and binary tree.2024-09-08
725. Split Linked List in Parts
Topic: Linked List
Difficulty: Medium
Problem:
Given the
The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
Return an array of the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg
Constraints:
• The number of nodes in the list is in the range
•
•
725. Split Linked List in Parts
Topic: Linked List
Difficulty: Medium
Problem:
Given the
head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
Return an array of the
k parts.Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg
Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].
Example 2:
Image: https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg
Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
Constraints:
• The number of nodes in the list is in the range
[0, 1000].•
0 <= Node.val <= 1000•
1 <= k <= 502024-09-09
2326. Spiral Matrix IV
Topic: Array, Linked List, Matrix, Simulation
Difficulty: Medium
Problem:
You are given two integers
You are also given the
Generate an
Return the generated matrix.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg
Constraints:
•
•
• The number of nodes in the list is in the range
•
2326. Spiral Matrix IV
Topic: Array, Linked List, Matrix, Simulation
Difficulty: Medium
Problem:
You are given two integers
m and n, which represent the dimensions of a matrix.You are also given the
head of a linked list of integers.Generate an
m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1.Return the generated matrix.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg
Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
Explanation: The diagram above shows how the values are printed in the matrix.
Note that the remaining spaces in the matrix are filled with -1.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg
Input: m = 1, n = 4, head = [0,1,2]
Output: [[0,1,2,-1]]
Explanation: The diagram above shows how the values are printed from left to right in the matrix.
The last space in the matrix is set to -1.
Constraints:
•
1 <= m, n <= 10^5•
1 <= m * n <= 10^5• The number of nodes in the list is in the range
[1, m * n].•
0 <= Node.val <= 10002024-09-10
2807. Insert Greatest Common Divisors in Linked List
Topic: Linked List, Math, Number Theory
Difficulty: Medium
Problem:
Given the head of a linked list
Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.
Return the linked list after insertion.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png
Constraints:
• The number of nodes in the list is in the range
•
2807. Insert Greatest Common Divisors in Linked List
Topic: Linked List, Math, Number Theory
Difficulty: Medium
Problem:
Given the head of a linked list
head, in which each node contains an integer value.Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.
Return the linked list after insertion.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png
Input: head = [18,6,10,3]
Output: [18,6,6,2,10,1,3]
Explanation: The 1^st diagram denotes the initial linked list and the 2^nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
- We insert the greatest common divisor of 18 and 6 = 6 between the 1^st and the 2^nd nodes.
- We insert the greatest common divisor of 6 and 10 = 2 between the 2^nd and the 3^rd nodes.
- We insert the greatest common divisor of 10 and 3 = 1 between the 3^rd and the 4^th nodes.
There are no more adjacent nodes, so we return the linked list.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png
Input: head = [7]
Output: [7]
Explanation: The 1^st diagram denotes the initial linked list and the 2^nd diagram denotes the linked list after inserting the new nodes.
There are no pairs of adjacent nodes, so we return the initial linked list.
Constraints:
• The number of nodes in the list is in the range
[1, 5000].•
1 <= Node.val <= 10002024-09-11
2220. Minimum Bit Flips to Convert Number
Topic: Bit Manipulation
Difficulty: Easy
Problem:
A bit flip of a number
• For example, for
Given two integers
Example 1:
Example 2:
Constraints:
•
2220. Minimum Bit Flips to Convert Number
Topic: Bit Manipulation
Difficulty: Easy
Problem:
A bit flip of a number
x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.• For example, for
x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.Given two integers
start and goal, return the minimum number of bit flips to convert start to goal.Example 1:
Input: start = 10, goal = 7
Output: 3
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -> 1011.
- Flip the third bit from the right: 1011 -> 1111.
- Flip the fourth bit from the right: 1111 -> 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
Example 2:
Input: start = 3, goal = 4
Output: 3
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -> 010.
- Flip the second bit from the right: 010 -> 000.
- Flip the third bit from the right: 000 -> 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
Constraints:
•
0 <= start, goal <= 10^92024-09-12
1684. Count the Number of Consistent Strings
Topic: Array, Hash Table, String, Bit Manipulation, Counting
Difficulty: Easy
Problem:
You are given a string
Return the number of consistent strings in the array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• The characters in
•
1684. Count the Number of Consistent Strings
Topic: Array, Hash Table, String, Bit Manipulation, Counting
Difficulty: Easy
Problem:
You are given a string
allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.Return the number of consistent strings in the array
words.Example 1:
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
Output: 2
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Example 2:
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
Output: 7
Explanation: All strings are consistent.
Example 3:
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
Output: 4
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
Constraints:
•
1 <= words.length <= 10^4•
1 <= allowed.length <= 26•
1 <= words[i].length <= 10• The characters in
allowed are distinct.•
words[i] and allowed contain only lowercase English letters.2024-09-13
1310. XOR Queries of a Subarray
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
You are given an array
For each query
Return an array
Example 1:
Example 2:
Constraints:
•
•
•
•
1310. XOR Queries of a Subarray
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
You are given an array
arr of positive integers. You are also given the array queries where queries[i] = [left_i, right_i].For each query
i compute the XOR of elements from left_i to right_i (that is, arr[left_i] XOR arr[left_i + 1] XOR ... XOR arr[right_i] ).Return an array
answer where answer[i] is the answer to the i^th query.Example 1:
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output: [2,7,14,8]
Explanation:
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
Example 2:
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
Output: [8,0,4,4]
Constraints:
•
1 <= arr.length, queries.length <= 3 * 10^4•
1 <= arr[i] <= 10^9•
queries[i].length == 2•
0 <= left_i <= right_i < arr.length2024-09-14
2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
Consider a non-empty subarray from
• In other words, let
Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
nums of size n.Consider a non-empty subarray from
nums that has the maximum possible bitwise AND.• In other words, let
k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,3,2,2]
Output: 2
Explanation:
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
Example 2:
Input: nums = [1,2,3,4]
Output: 1
Explanation:
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62024-09-15
1371. Find the Longest Substring Containing Vowels in Even Counts
Topic: Hash Table, String, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given the string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1371. Find the Longest Substring Containing Vowels in Even Counts
Topic: Hash Table, String, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given the string
s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.Example 1:
Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
Example 2:
Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.
Example 3:
Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.
Constraints:
•
1 <= s.length <= 5 x 10^5•
s contains only lowercase English letters.2024-09-16
539. Minimum Time Difference
Topic: Array, Math, String, Sorting
Difficulty: Medium
Problem:
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
Example 1:
Example 2:
Constraints:
•
•
539. Minimum Time Difference
Topic: Array, Math, String, Sorting
Difficulty: Medium
Problem:
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
Example 1:
Input: timePoints = ["23:59","00:00"]
Output: 1
Example 2:
Input: timePoints = ["00:00","23:59","00:00"]
Output: 0
Constraints:
•
2 <= timePoints.length <= 2 * 10^4•
timePoints[i] is in the format "HH:MM".2024-09-17
884. Uncommon Words from Two Sentences
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
A sentence is a string of single-space separated words where each word consists only of lowercase letters.
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Given two sentences
Example 1:
Input: s1 = "this apple is sweet", s2 = "this apple is sour"
Output: "sweet","sour"
Explanation:
The word
Example 2:
Input: s1 = "apple apple", s2 = "banana"
Output: "banana"
Constraints:
•
•
•
• All the words in
884. Uncommon Words from Two Sentences
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
A sentence is a string of single-space separated words where each word consists only of lowercase letters.
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Given two sentences
s1 and s2, return a list of all the uncommon words. You may return the answer in any order.Example 1:
Input: s1 = "this apple is sweet", s2 = "this apple is sour"
Output: "sweet","sour"
Explanation:
The word
"sweet" appears only in s1, while the word "sour" appears only in s2.Example 2:
Input: s1 = "apple apple", s2 = "banana"
Output: "banana"
Constraints:
•
1 <= s1.length, s2.length <= 200•
s1 and s2 consist of lowercase English letters and spaces.•
s1 and s2 do not have leading or trailing spaces.• All the words in
s1 and s2 are separated by a single space.2024-09-18
179. Largest Number
Topic: Array, String, Greedy, Sorting
Difficulty: Medium
Problem:
Given a list of non-negative integers
Since the result may be very large, so you need to return a string instead of an integer.
Example 1:
Example 2:
Constraints:
•
•
179. Largest Number
Topic: Array, String, Greedy, Sorting
Difficulty: Medium
Problem:
Given a list of non-negative integers
nums, arrange them such that they form the largest number and return it.Since the result may be very large, so you need to return a string instead of an integer.
Example 1:
Input: nums = [10,2]
Output: "210"
Example 2:
Input: nums = [3,30,34,5,9]
Output: "9534330"
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 10^92024-09-19
241. Different Ways to Add Parentheses
Topic: Math, String, Dynamic Programming, Recursion, Memoization
Difficulty: Medium
Problem:
Given a string
The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed
Example 1:
Example 2:
Constraints:
•
•
• All the integer values in the input expression are in the range
241. Different Ways to Add Parentheses
Topic: Math, String, Dynamic Programming, Recursion, Memoization
Difficulty: Medium
Problem:
Given a string
expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed
10^4.Example 1:
Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input: expression = "2*3-4*5"
Output: [-34,-14,-10,-10,10]
Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Constraints:
•
1 <= expression.length <= 20•
expression consists of digits and the operator '+', '-', and '*'.• All the integer values in the input expression are in the range
[0, 99].2024-09-20
214. Shortest Palindrome
Topic: String, Rolling Hash, String Matching, Hash Function
Difficulty: Hard
Problem:
You are given a string
Return the shortest palindrome you can find by performing this transformation.
Example 1:
Example 2:
Constraints:
•
•
214. Shortest Palindrome
Topic: String, Rolling Hash, String Matching, Hash Function
Difficulty: Hard
Problem:
You are given a string
s. You can convert s to a palindrome by adding characters in front of it.Return the shortest palindrome you can find by performing this transformation.
Example 1:
Input: s = "aacecaaa"
Output: "aaacecaaa"
Example 2:
Input: s = "abcd"
Output: "dcbabcd"
Constraints:
•
0 <= s.length <= 5 * 10^4•
s consists of lowercase English letters only.2024-09-21
386. Lexicographical Numbers
Topic: Depth-First Search, Trie
Difficulty: Medium
Problem:
Given an integer
You must write an algorithm that runs in
Example 1:
Example 2:
Constraints:
•
386. Lexicographical Numbers
Topic: Depth-First Search, Trie
Difficulty: Medium
Problem:
Given an integer
n, return all the numbers in the range [1, n] sorted in lexicographical order.You must write an algorithm that runs in
O(n) time and uses O(1) extra space. Example 1:
Input: n = 13
Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
Example 2:
Input: n = 2
Output: [1,2]
Constraints:
•
1 <= n <= 5 * 10^42024-09-22
440. K-th Smallest in Lexicographical Order
Topic: Trie
Difficulty: Hard
Problem:
Given two integers
Example 1:
Example 2:
Constraints:
•
440. K-th Smallest in Lexicographical Order
Topic: Trie
Difficulty: Hard
Problem:
Given two integers
n and k, return the k^th lexicographically smallest integer in the range [1, n].Example 1:
Input: n = 13, k = 2
Output: 10
Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
Example 2:
Input: n = 1, k = 1
Output: 1
Constraints:
•
1 <= k <= n <= 10^92024-09-23
2707. Extra Characters in a String
Topic: Array, Hash Table, String, Dynamic Programming, Trie
Difficulty: Medium
Problem:
You are given a 0-indexed string
Return the minimum number of extra characters left over if you break up
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2707. Extra Characters in a String
Topic: Array, Hash Table, String, Dynamic Programming, Trie
Difficulty: Medium
Problem:
You are given a 0-indexed string
s and a dictionary of words dictionary. You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary. There may be some extra characters in s which are not present in any of the substrings.Return the minimum number of extra characters left over if you break up
s optimally.Example 1:
Input: s = "leetscode", dictionary = ["leet","code","leetcode"]
Output: 1
Explanation: We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
Example 2:
Input: s = "sayhelloworld", dictionary = ["hello","world"]
Output: 3
Explanation: We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
Constraints:
•
1 <= s.length <= 50•
1 <= dictionary.length <= 50•
1 <= dictionary[i].length <= 50•
dictionary[i] and s consists of only lowercase English letters•
dictionary contains distinct words2024-09-24
3043. Find the Length of the Longest Common Prefix
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
You are given two arrays with positive integers
A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example,
A common prefix of two integers
You need to find the length of the longest common prefix between all pairs of integers
Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return
Example 1:
Example 2:
Constraints:
•
•
3043. Find the Length of the Longest Common Prefix
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
You are given two arrays with positive integers
arr1 and arr2.A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example,
123 is a prefix of the integer 12345, while 234 is not.A common prefix of two integers
a and b is an integer c, such that c is a prefix of both a and b. For example, 5655359 and 56554 have a common prefix 565 while 1223 and 43456 do not have a common prefix.You need to find the length of the longest common prefix between all pairs of integers
(x, y) such that x belongs to arr1 and y belongs to arr2.Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return
0.Example 1:
Input: arr1 = [1,10,100], arr2 = [1000]
Output: 3
Explanation: There are 3 pairs (arr1[i], arr2[j]):
- The longest common prefix of (1, 1000) is 1.
- The longest common prefix of (10, 1000) is 10.
- The longest common prefix of (100, 1000) is 100.
The longest common prefix is 100 with a length of 3.
Example 2:
Input: arr1 = [1,2,3], arr2 = [4,4,4]
Output: 0
Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
Note that common prefixes between elements of the same array do not count.
Constraints:
•
1 <= arr1.length, arr2.length <= 5 * 10^4•
1 <= arr1[i], arr2[i] <= 10^82024-09-25
2416. Sum of Prefix Scores of Strings
Topic: Array, String, Trie, Counting
Difficulty: Hard
Problem:
You are given an array
We define the score of a string
• For example, if
Return an array
Note that a string is considered as a prefix of itself.
Example 1:
Example 2:
Constraints:
•
•
•
2416. Sum of Prefix Scores of Strings
Topic: Array, String, Trie, Counting
Difficulty: Hard
Problem:
You are given an array
words of size n consisting of non-empty strings.We define the score of a string
word as the number of strings words[i] such that word is a prefix of words[i].• For example, if
words = ["a", "ab", "abc", "cab"], then the score of "ab" is 2, since "ab" is a prefix of both "ab" and "abc".Return an array
answer of size n where answer[i] is the sum of scores of every non-empty prefix of words[i].Note that a string is considered as a prefix of itself.
Example 1:
Input: words = ["abc","ab","bc","b"]
Output: [5,4,3,2]
Explanation: The answer for each string is the following:
- "abc" has 3 prefixes: "a", "ab", and "abc".
- There are 2 strings with the prefix "a", 2 strings with the prefix "ab", and 1 string with the prefix "abc".
The total is answer[0] = 2 + 2 + 1 = 5.
- "ab" has 2 prefixes: "a" and "ab".
- There are 2 strings with the prefix "a", and 2 strings with the prefix "ab".
The total is answer[1] = 2 + 2 = 4.
- "bc" has 2 prefixes: "b" and "bc".
- There are 2 strings with the prefix "b", and 1 string with the prefix "bc".
The total is answer[2] = 2 + 1 = 3.
- "b" has 1 prefix: "b".
- There are 2 strings with the prefix "b".
The total is answer[3] = 2.
Example 2:
Input: words = ["abcd"]
Output: [4]
Explanation:
"abcd" has 4 prefixes: "a", "ab", "abc", and "abcd".
Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4.
Constraints:
•
1 <= words.length <= 1000•
1 <= words[i].length <= 1000•
words[i] consists of lowercase English letters.2024-09-26
729. My Calendar I
Topic: Array, Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers
Implement the
•
•
Example 1:
Constraints:
•
• At most
729. My Calendar I
Topic: Array, Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers
start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.Implement the
MyCalendar class:•
MyCalendar() Initializes the calendar object.•
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.Example 1:
Input
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output
[null, true, false, true]
Explanation
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
Constraints:
•
0 <= start < end <= 10^9• At most
1000 calls will be made to book.2024-09-27
731. My Calendar II
Topic: Array, Binary Search, Design, Segment Tree, Prefix Sum, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers
Implement the
•
•
Example 1:
Constraints:
•
• At most
731. My Calendar II
Topic: Array, Binary Search, Design, Segment Tree, Prefix Sum, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers
start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.Implement the
MyCalendarTwo class:•
MyCalendarTwo() Initializes the calendar object.•
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.Example 1:
Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]
Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
Constraints:
•
0 <= start < end <= 10^9• At most
1000 calls will be made to book.