Skip to content

Commit cb2768e

Browse files
Merge branch 'main' into problems-3583-3772-3771-3770-3769-3768-3767-3766-3765-3764-3762-3761-3760
2 parents 3089bef + fec1102 commit cb2768e

File tree

7 files changed

+210
-150
lines changed

7 files changed

+210
-150
lines changed

data/book-sets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
1304, 1318, 1337, 1372, 1423, 1431, 1448, 1456, 1466, 1480, 1493, 1515, 1523, 1528, 1557, 1584, 1657, 1672, 1679, 1704, 1732, 1768, 1798, 1920,
6262
1925, 1926, 1929, 1957, 1963, 2011, 2095, 2119, 2130, 2211, 2215, 2300, 2336, 2352, 2390, 2419, 2462, 2542, 2627, 2703, 2723, 2769, 2807, 2862,
6363
2879, 2884, 2888, 2894, 2942, 3100, 3110, 3133, 3164, 3190, 3197, 3228, 3291, 3320, 3351, 3380, 3381, 3413, 3424, 3432, 3444, 3471, 3512, 3522,
64-
3583, 3602, 3603, 3606, 3607, 3608, 3622, 3623, 3625, 3663, 3668, 3678, 3683, 3688, 3692, 3697, 3701, 3707, 3712, 3718, 3722, 3723, 3724, 3726,
65-
3727, 3728, 3731, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3759, 3760,
66-
3761, 3764, 3765, 3766, 3767, 3768, 3770, 3771, 3772
64+
3602, 3603, 3606, 3607, 3608, 3622, 3623, 3625, 3663, 3668, 3678, 3683, 3688, 3692, 3697, 3701, 3707, 3712, 3718, 3722, 3723, 3724, 3726, 3727,
65+
3728, 3731, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3759, 3760, 3761,
66+
3762, 3764, 3765, 3766, 3767, 3768, 3769, 3770, 3771, 3772
6767
]
6868
},
6969
{"title": "Visualization", "description": "", "tags": [], "problems": [1, 2, 11, 1431, 1679, 1768, 1798, 2215, 3603, 3622, 3623]},

explanations/1071/en.md

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
## 1071. Greatest Common Divisor of Strings [Easy]
1+
## Explanation
22

3-
https://leetcode.com/problems/greatest-common-divisor-of-strings
3+
### Strategy (The "Why")
44

5-
## Description
6-
For two strings `s` and `t`, we say "t divides s" if and only if `s = t + t + ... + t` (i.e., t is concatenated with itself one or more times).
5+
Given two strings `str1` and `str2`, we need to find the largest string `x` such that `x` divides both `str1` and `str2`. A string `t` divides `s` if `s` can be formed by concatenating `t` multiple times.
76

8-
Given two strings `str1` and `str2`, return the largest string `x` such that `x` divides both `str1` and `str2`.
7+
**1.1 Constraints & Complexity:**
98

10-
**Examples**
11-
```text
12-
Input: str1 = "ABCABC", str2 = "ABC"
13-
Output: "ABC"
9+
- **Input Size:** Each string can have length between 1 and 1000.
10+
- **Value Range:** Strings consist of English uppercase letters.
11+
- **Time Complexity:** O(n + m) where n and m are the lengths of str1 and str2. We check string equality and compute GCD of lengths.
12+
- **Space Complexity:** O(min(n, m)) - We store the GCD string, which is at most the length of the shorter string.
13+
- **Edge Case:** If the strings don't share a common divisor pattern (str1 + str2 ≠ str2 + str1), return empty string.
1414

15-
Input: str1 = "ABABAB", str2 = "ABAB"
16-
Output: "AB"
15+
**1.2 High-level approach:**
1716

18-
Input: str1 = "LEET", str2 = "CODE"
19-
Output: ""
20-
```
17+
The goal is to find the longest string that can be repeated to form both str1 and str2. This is analogous to finding the greatest common divisor (GCD) of two numbers, but for strings.
2118

22-
**Constraints**
23-
- 1 <= str1.length, str2.length <= 1000
24-
- str1 and str2 consist of English uppercase letters.
19+
![Greatest Common Divisor of Strings](https://assets.leetcode.com/uploads/2020/01/31/gcd-of-strings.png)
2520

26-
> Try to find the greatest common divisor (GCD) of the lengths of the two strings, and check if the substring of that length divides both strings.
21+
The key insight is that if a common divisor exists, then `str1 + str2` must equal `str2 + str1`. The length of the GCD string will be the GCD of the lengths of the two strings.
2722

28-
## Explanation
23+
**1.3 Brute force vs. optimized strategy:**
24+
25+
- **Brute Force:** Try all possible prefixes of the shorter string, check if each divides both strings. This would be O(n² × m) where we check each prefix and verify it divides both strings.
26+
- **Optimized Strategy (GCD of Lengths):** First verify that str1 + str2 == str2 + str1. If true, compute the GCD of the string lengths, and return the prefix of that length. This is O(n + m) for the check plus O(log(min(n, m))) for GCD computation.
27+
- **Why it's better:** We avoid checking every possible prefix by using the mathematical property that the GCD string length must be the GCD of the two string lengths.
28+
29+
**1.4 Decomposition:**
30+
31+
1. Check if str1 + str2 equals str2 + str1. If not, return empty string (no common divisor exists).
32+
2. Compute the GCD of the lengths of str1 and str2 using the Euclidean algorithm.
33+
3. Return the prefix of str1 (or str2) with length equal to the GCD.
2934

30-
### Strategy
31-
Let's restate the problem: Given two strings, find the largest string that can be repeated to form both strings exactly. This is a string manipulation problem with a mathematical twist (using the greatest common divisor, or GCD).
35+
### Steps (The "How")
3236

33-
- **What is given?** Two strings, `str1` and `str2`.
34-
- **What is being asked?** Find the largest string x such that both str1 and str2 are made by repeating x some number of times.
35-
- **Constraints:** Both strings are non-empty, up to 1000 characters, and only contain uppercase English letters.
36-
- **Edge cases:** If the strings cannot be built from the same repeating substring, the answer is an empty string.
37+
**2.1 Initialization & Example Setup:**
3738

38-
Suppose you have two strings, and you want to find the biggest chunk that can be repeated to make both strings. Think of it like finding the biggest building block that fits perfectly into **both**.
39+
Let's use the example: `str1 = "ABABAB"`, `str2 = "ABAB"`
3940

40-
First, you check if both strings can be built by repeating the same substring, because if they can't, there's no **common divisor** string. You do this by comparing `str1 + str2` and `str2 + str1` — if they're not the same, it means the order of repetition doesn't match, so no common divisor exists. This check is important because it quickly rules out impossible cases, saving you time and effort.
41+
We first check: `str1 + str2 = "ABABABABAB"` and `str2 + str1 = "ABABABABAB"`. They are equal, so a common divisor exists.
4142

42-
Next, you use the greatest common divisor (GCD) of the lengths of the two strings. You do this because the largest possible repeating substring must fit evenly into both strings, and the GCD gives you the largest length that divides both. This is a mathematical shortcut that ensures you only check the most promising candidate.
43+
**2.2 Start Checking:**
4344

44-
Finally, you take the substring of that length from the start of either string and check if repeating it forms both strings. This works because if a substring can build both strings by repetition, it must be the answer.
45+
We compute the GCD of the lengths:
46+
- `len(str1) = 6`
47+
- `len(str2) = 4`
48+
- GCD(6, 4) = 2
4549

46-
**High-level plan:**
47-
1. If `str1 + str2 != str2 + str1`, there is no common divisor string. Return "".
48-
2. Otherwise, the answer is the substring of length gcd(len(str1), len(str2)) from the start of either string.
50+
**2.3 Trace Walkthrough:**
4951

50-
> Try to find the greatest common divisor (GCD) of the lengths of the two strings, and check if the substring of that length divides both strings.
52+
| Step | Operation | a | b | Result |
53+
|------|-----------|---|---|--------|
54+
| 1 | Initial | 6 | 4 | - |
55+
| 2 | GCD(6, 4) | 6 | 4 | GCD(4, 6 % 4 = 2) |
56+
| 3 | GCD(4, 2) | 4 | 2 | GCD(2, 4 % 2 = 0) |
57+
| 4 | GCD(2, 0) | 2 | 0 | Return 2 |
5158

52-
### Steps
53-
1. **Check for a valid repeating pattern:**
54-
- Concatenate `str1 + str2` and `str2 + str1`. If they are not equal, it means the two strings cannot be built from the same repeating substring. For example:
55-
- `str1 = "ABCABC", str2 = "ABC"``"ABCABCABC" == "ABCABCABC"` (valid)
56-
- `str1 = "LEET", str2 = "CODE"``"LEETCODE" != "CODELEET"` (invalid)
59+
**2.4 Explanation:**
5760

58-
2. **Find the GCD of the lengths:**
59-
- If the pattern is valid, compute the greatest common divisor (GCD) of the lengths of str1 and str2. This gives the largest possible length for the repeating substring.
60-
- Example: `str1 = "ABABAB"` (length 6), `str2 = "ABAB"` (length 4). GCD(6, 4) = 2.
61+
- The GCD of 6 and 4 is 2, so the GCD string has length 2.
62+
- We return `str1[:2] = "AB"`.
63+
- Verification: "AB" × 3 = "ABABAB" = str1, and "AB" × 2 = "ABAB" = str2.
6164

62-
3. **Extract the substring:**
63-
- Take the substring of length GCD from the start of either string. This is the candidate for the greatest common divisor string.
64-
- Example: `str1 = "ABABAB", str2 = "ABAB"`. GCD = 2, so substring = "AB".
65+
**2.5 Return Result:**
6566

66-
4. **Return the result:**
67-
- If the above checks pass, return the substring. Otherwise, return an empty string.
67+
We return `"AB"`, which is the largest string that divides both str1 and str2.
6868

69-
> The key insight is that if two strings are made by repeating the same substring, their concatenations in both orders must be equal. This is a quick way to check for a common divisor string.
69+
> **Note:** The condition `str1 + str2 == str2 + str1` is necessary and sufficient for a common divisor to exist. This is a mathematical property of string divisibility.

explanations/1431/en.md

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,71 @@
1-
## 1431. Kids With the Greatest Number of Candies [Easy]
1+
## Explanation
22

3-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
3+
### Strategy (The "Why")
44

5-
## Description
6-
There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the `i`th kid has, and an integer `extraCandies`, denoting the number of extra candies that you have.
5+
Given an array `candies` representing the number of candies each kid has, and `extraCandies` extra candies, we need to determine for each kid whether giving them all the extra candies would make them have the greatest number of candies.
76

8-
Return a boolean array `result` of length `n`, where `result[i]` is `true` if, after giving the `i`th kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or false otherwise.
7+
**1.1 Constraints & Complexity:**
98

10-
Note that multiple kids can have the greatest number of candies.
9+
- **Input Size:** The number of kids n is between 2 and 100.
10+
- **Value Range:** Each kid has between 1 and 100 candies, and extraCandies is between 1 and 50.
11+
- **Time Complexity:** O(n) - We iterate through the candies array twice: once to find the maximum, and once to build the result.
12+
- **Space Complexity:** O(n) - We create a result list of length n.
13+
- **Edge Case:** If all kids have the same number of candies, all will be true (since adding extraCandies to any will make them have the most).
1114

12-
**Examples**
15+
**1.2 High-level approach:**
1316

14-
```text
15-
Input: candies = [2,3,5,1,3], extraCandies = 3
16-
Output: [true,true,true,false,true]
17-
Explanation: If you give all extraCandies to:
18-
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
19-
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
20-
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
21-
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
22-
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
17+
The goal is to check, for each kid, if their current candies plus the extra candies would be greater than or equal to the maximum number of candies any kid currently has.
2318

24-
Input: candies = [4,2,1,1,2], extraCandies = 1
25-
Output: [true,false,false,false,false]
19+
![Kids With Candies](https://assets.leetcode.com/uploads/2020/06/14/kids-with-candies.png)
2620

27-
Input: candies = [12,1,12], extraCandies = 10
28-
Output: [true,false,true]
29-
```
21+
We first find the maximum number of candies, then for each kid, we check if `candies[i] + extraCandies >= max_candies`.
3022

31-
**Constraints**
23+
**1.3 Brute force vs. optimized strategy:**
3224

33-
```text
34-
- n == candies.length
35-
- 2 <= n <= 100
36-
- 1 <= candies[i] <= 100
37-
- 1 <= extraCandies <= 50
38-
```
25+
- **Brute Force:** For each kid, add extraCandies to their count, then check if this new count is greater than all other kids' counts. This would be O(n²) because for each kid we compare with all others.
26+
- **Optimized Strategy (Two-Pass):** First pass: find the maximum number of candies. Second pass: for each kid, check if their candies plus extraCandies is >= the maximum. This is O(n).
27+
- **Why it's better:** By finding the maximum first, we only need one comparison per kid in the second pass, reducing time complexity from O(n²) to O(n).
3928

40-
## Hint
41-
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.
29+
**1.4 Decomposition:**
4230

43-
## Explanation
44-
First, you want to know the highest number of candies any kid currently has. This is important because you need a reference point to see if giving extra candies to a kid will make them "the greatest."
31+
1. Find the maximum number of candies in the array.
32+
2. Initialize an empty result list.
33+
3. For each kid, check if their candies plus extraCandies is greater than or equal to the maximum.
34+
4. Append True or False to the result list accordingly.
35+
5. Return the result list.
36+
37+
### Steps (The "How")
38+
39+
**2.1 Initialization & Example Setup:**
40+
41+
Let's use the example: `candies = [2, 3, 5, 1, 3]`, `extraCandies = 3`
42+
43+
We initialize:
44+
- `max_candies = 0` (will find maximum)
45+
- `res = []` (result list)
46+
47+
**2.2 Start Checking:**
48+
49+
First, we find the maximum: `max_candies = max(candies) = 5`
50+
51+
**2.3 Trace Walkthrough:**
52+
53+
| Kid Index | Current Candies | Current + Extra | >= Max (5)? | res |
54+
|-----------|----------------|-----------------|-------------|-----|
55+
| 0 | 2 | 2 + 3 = 5 | Yes (5 >= 5) | [True] |
56+
| 1 | 3 | 3 + 3 = 6 | Yes (6 >= 5) | [True, True] |
57+
| 2 | 5 | 5 + 3 = 8 | Yes (8 >= 5) | [True, True, True] |
58+
| 3 | 1 | 1 + 3 = 4 | No (4 < 5) | [True, True, True, False] |
59+
| 4 | 3 | 3 + 3 = 6 | Yes (6 >= 5) | [True, True, True, False, True] |
60+
61+
**2.4 Increment and Loop:**
62+
63+
- For each kid, we calculate `candies[i] + extraCandies`.
64+
- We compare this sum with `max_candies`.
65+
- If the sum is >= max_candies, we append True; otherwise, we append False.
66+
67+
**2.5 Return Result:**
4568

46-
For each kid, you add the `extraCandies` to their current amount. You do this because you want to see if, after the bonus, they can reach or beat the current maximum. If they do, you mark them as `True` in our answer list; otherwise, False.
69+
We return `[True, True, True, False, True]`, indicating that kids at indices 0, 1, 2, and 4 would have the greatest number of candies after receiving the extra candies.
4770

48-
You only need to find the maximum once, and then just compare each kid's total to it. Don't need to recalculate the maximum for every kid.
71+
> **Note:** We compare with the original maximum, not the maximum after giving extra candies. This is correct because if a kid's candies plus extraCandies is >= the original maximum, they will have at least as many as the kid who originally had the most.

0 commit comments

Comments
 (0)