Skip to content

Commit bef452b

Browse files
authored
Added tasks 2262, 2264, 2265, 2266, 2267, 2269, 2270, 2274, 2299.
1 parent 1294dd6 commit bef452b

File tree

10 files changed

+724
-0
lines changed
  • src/main/java/g2201_2300
    • s2262_total_appeal_of_a_string
    • s2264_largest_3_same_digit_number_in_string
    • s2265_count_nodes_equal_to_average_of_subtree
    • s2266_count_number_of_texts
    • s2267_check_if_there_is_a_valid_parentheses_string_path
    • s2269_find_the_k_beauty_of_a_number
    • s2270_number_of_ways_to_split_array
    • s2274_maximum_consecutive_floors_without_special_floors
    • s2299_strong_password_checker_ii

10 files changed

+724
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,18 @@
13341334
| # | Title | Difficulty | Tag | Time, ms | Time, %
13351335
|------|----------------|-------------|-------------|----------|--------
13361336
| 2300 |[Successful Pairs of Spells and Potions](src/main/java/g2201_2300/s2300_successful_pairs_of_spells_and_potions)| Medium | Array, Two_Pointers, Binary_Search, Sorting | 85 | 71.70
1337+
| 2299 |[Strong Password Checker II](src/main/java/g2201_2300/s2299_strong_password_checker_ii)| Easy | String | 1 | 97.32
13371338
| 2293 |[Min Max Game](src/main/java/g2201_2300/s2293_min_max_game)| Easy | Array, Simulation | 1 | 90.39
13381339
| 2286 |[Booking Concert Tickets in Groups](src/main/java/g2201_2300/s2286_booking_concert_tickets_in_groups)| Hard | Binary_Search, Design, Binary_Indexed_Tree, Segment_Tree | 283 | 67.08
13391340
| 2280 |[Minimum Lines to Represent a Line Chart](src/main/java/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart)| Medium | Array, Math, Geometry, Sorting, Number_Theory | 40 | 96.09
1341+
| 2274 |[Maximum Consecutive Floors Without Special Floors](src/main/java/g2201_2300/s2274_maximum_consecutive_floors_without_special_floors)| Medium | Array, Sorting | 33 | 99.36
1342+
| 2270 |[Number of Ways to Split Array](src/main/java/g2201_2300/s2270_number_of_ways_to_split_array)| Medium | Array, Prefix_Sum | 4 | 77.55
1343+
| 2269 |[Find the K-Beauty of a Number](src/main/java/g2201_2300/s2269_find_the_k_beauty_of_a_number)| Easy | Math, String, Sliding_Window | 2 | 38.88
1344+
| 2267 |[Check if There Is a Valid Parentheses String Path](src/main/java/g2201_2300/s2267_check_if_there_is_a_valid_parentheses_string_path)| Hard | Array, Dynamic_Programming, Matrix | 93 | 77.48
1345+
| 2266 |[Count Number of Texts](src/main/java/g2201_2300/s2266_count_number_of_texts)| Medium | Hash_Table, Math, String, Dynamic_Programming | 38 | 81.43
1346+
| 2265 |[Count Nodes Equal to Average of Subtree](src/main/java/g2201_2300/s2265_count_nodes_equal_to_average_of_subtree)| Medium | Tree, Depth_First_Search, Binary_Tree | 1 | 99.12
1347+
| 2264 |[Largest 3-Same-Digit Number in String](src/main/java/g2201_2300/s2264_largest_3_same_digit_number_in_string)| Easy | String | 3 | 74.57
1348+
| 2262 |[Total Appeal of A String](src/main/java/g2201_2300/s2262_total_appeal_of_a_string)| Hard | Hash_Table, String, Dynamic_Programming | 6 | 97.92
13401349
| 2260 |[Minimum Consecutive Cards to Pick Up](src/main/java/g2201_2300/s2260_minimum_consecutive_cards_to_pick_up)| Medium | Array, Hash_Table, Sliding_Window | 50 | 97.04
13411350
| 2259 |[Remove Digit From Number to Maximize Result](src/main/java/g2201_2300/s2259_remove_digit_from_number_to_maximize_result)| Easy | String, Greedy, Enumeration | 1 | 97.73
13421351
| 2257 |[Count Unguarded Cells in the Grid](src/main/java/g2201_2300/s2257_count_unguarded_cells_in_the_grid)| Medium | Array, Matrix, Simulation | 32 | 70.28
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## 2262\. Total Appeal of A String
2+
3+
Hard
4+
5+
The **appeal** of a string is the number of **distinct** characters found in the string.
6+
7+
* For example, the appeal of `"abbca"` is `3` because it has `3` distinct characters: `'a'`, `'b'`, and `'c'`.
8+
9+
Given a string `s`, return _the **total appeal of all of its **substrings**.**_
10+
11+
A **substring** is a contiguous sequence of characters within a string.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "abbca"
16+
17+
**Output:** 28
18+
19+
**Explanation:** The following are the substrings of "abbca":
20+
21+
- Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.
22+
23+
- Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.
24+
25+
- Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7.
26+
27+
- Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6.
28+
29+
- Substrings of length 5: "abbca" has an appeal of 3. The sum is 3.
30+
31+
The total sum is 5 + 7 + 7 + 6 + 3 = 28.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "code"
36+
37+
**Output:** 20
38+
39+
**Explanation:** The following are the substrings of "code":
40+
41+
- Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.
42+
43+
- Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6.
44+
45+
- Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6.
46+
47+
- Substrings of length 4: "code" has an appeal of 4. The sum is 4.
48+
49+
The total sum is 4 + 6 + 6 + 4 = 20.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= s.length <= 10<sup>5</sup></code>
54+
* `s` consists of lowercase English letters.
55+
56+
## Solution
57+
58+
```java
59+
import java.util.Arrays;
60+
61+
public class Solution {
62+
public long appealSum(String s) {
63+
int len = s.length();
64+
int[] lastPos = new int[26];
65+
Arrays.fill(lastPos, -1);
66+
long res = 0;
67+
for (int i = 0; i < len; i++) {
68+
int idx = s.charAt(i) - 'a';
69+
res += (i - lastPos[idx]) * (len - i);
70+
lastPos[idx] = i;
71+
}
72+
return res;
73+
}
74+
}
75+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 2264\. Largest 3-Same-Digit Number in String
2+
3+
Easy
4+
5+
You are given a string `num` representing a large integer. An integer is **good** if it meets the following conditions:
6+
7+
* It is a **substring** of `num` with length `3`.
8+
* It consists of only one unique digit.
9+
10+
Return _the **maximum good** integer as a **string** or an empty string_ `""` _if no such integer exists_.
11+
12+
Note:
13+
14+
* A **substring** is a contiguous sequence of characters within a string.
15+
* There may be **leading zeroes** in `num` or a good integer.
16+
17+
**Example 1:**
18+
19+
**Input:** num = "6**777**133339"
20+
21+
**Output:** "777"
22+
23+
**Explanation:** There are two distinct good integers: "777" and "333".
24+
25+
"777" is the largest, so we return "777".
26+
27+
**Example 2:**
28+
29+
**Input:** num = "23**000**19"
30+
31+
**Output:** "000"
32+
33+
**Explanation:** "000" is the only good integer.
34+
35+
**Example 3:**
36+
37+
**Input:** num = "42352338"
38+
39+
**Output:** ""
40+
41+
**Explanation:** No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
42+
43+
**Constraints:**
44+
45+
* `3 <= num.length <= 1000`
46+
* `num` only consists of digits.
47+
48+
## Solution
49+
50+
```java
51+
public class Solution {
52+
public String largestGoodInteger(String num) {
53+
String maxi = "000";
54+
int c = 0;
55+
for (int i = 0; i < num.length() - 2; i++) {
56+
String s = num.substring(i, i + 3);
57+
if (s.charAt(0) == s.charAt(1) && s.charAt(1) == s.charAt(2)) {
58+
if (s.compareTo(maxi) >= 0) {
59+
maxi = s;
60+
}
61+
++c;
62+
}
63+
}
64+
if (c == 0) {
65+
return "";
66+
}
67+
return maxi;
68+
}
69+
}
70+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## 2265\. Count Nodes Equal to Average of Subtree
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the number of nodes where the value of the node is equal to the **average** of the values in its **subtree**_.
6+
7+
**Note:**
8+
9+
* The **average** of `n` elements is the **sum** of the `n` elements divided by `n` and **rounded down** to the nearest integer.
10+
* A **subtree** of `root` is a tree consisting of `root` and all of its descendants.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png)
15+
16+
**Input:** root = [4,8,5,0,1,null,6]
17+
18+
**Output:** 5
19+
20+
**Explanation:**
21+
22+
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
23+
24+
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
25+
26+
For the node with value 0: The average of its subtree is 0 / 1 = 0.
27+
28+
For the node with value 1: The average of its subtree is 1 / 1 = 1.
29+
30+
For the node with value 6: The average of its subtree is 6 / 1 = 6.
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png)
35+
36+
**Input:** root = [1]
37+
38+
**Output:** 1
39+
40+
**Explanation:** For the node with value 1: The average of its subtree is 1 / 1 = 1.
41+
42+
**Constraints:**
43+
44+
* The number of nodes in the tree is in the range `[1, 1000]`.
45+
* `0 <= Node.val <= 1000`
46+
47+
## Solution
48+
49+
```java
50+
import com_github_leetcode.TreeNode;
51+
52+
public class Solution {
53+
private int ans = 0;
54+
55+
public int averageOfSubtree(TreeNode root) {
56+
dfs(root);
57+
return ans;
58+
}
59+
60+
private int[] dfs(TreeNode node) {
61+
if (node == null) {
62+
return new int[] {0, 0};
63+
}
64+
int[] left = dfs(node.left);
65+
int[] right = dfs(node.right);
66+
int currsum = left[0] + right[0] + node.val;
67+
int currcount = left[1] + right[1] + 1;
68+
if (currsum / currcount == node.val) {
69+
++ans;
70+
}
71+
return new int[] {currsum, currcount};
72+
}
73+
}
74+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## 2266\. Count Number of Texts
2+
3+
Medium
4+
5+
Alice is texting Bob using her phone. The **mapping** of digits to letters is shown in the figure below.
6+
7+
![](https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png)
8+
9+
In order to **add** a letter, Alice has to **press** the key of the corresponding digit `i` times, where `i` is the position of the letter in the key.
10+
11+
* For example, to add the letter `'s'`, Alice has to press `'7'` four times. Similarly, to add the letter `'k'`, Alice has to press `'5'` twice.
12+
* Note that the digits `'0'` and `'1'` do not map to any letters, so Alice **does not** use them.
13+
14+
However, due to an error in transmission, Bob did not receive Alice's text message but received a **string of pressed keys** instead.
15+
16+
* For example, when Alice sent the message `"bob"`, Bob received the string `"2266622"`.
17+
18+
Given a string `pressedKeys` representing the string received by Bob, return _the **total number of possible text messages** Alice could have sent_.
19+
20+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
21+
22+
**Example 1:**
23+
24+
**Input:** pressedKeys = "22233"
25+
26+
**Output:** 8
27+
28+
**Explanation:**
29+
30+
The possible text messages Alice could have sent are:
31+
32+
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
33+
34+
Since there are 8 possible messages, we return 8.
35+
36+
**Example 2:**
37+
38+
**Input:** pressedKeys = "222222222222222222222222222222222222"
39+
40+
**Output:** 82876089
41+
42+
**Explanation:** There are 2082876103 possible text messages Alice could have sent.
43+
44+
Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 2082876103 % (10<sup>9</sup> + 7) = 82876089.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= pressedKeys.length <= 10<sup>5</sup></code>
49+
* `pressedKeys` only consists of digits from `'2'` - `'9'`.
50+
51+
## Solution
52+
53+
```java
54+
@SuppressWarnings("java:S135")
55+
public class Solution {
56+
public int countTexts(String pressedKeys) {
57+
int len = pressedKeys.length();
58+
long dp0 = 1L;
59+
long dp1 = 0;
60+
long dp2 = 0;
61+
long dp3 = 0;
62+
long dp4;
63+
char[] keys = pressedKeys.toCharArray();
64+
int base = 1000000007;
65+
for (int i = 1; i < len; i++) {
66+
int r = keys[i] - '0';
67+
dp4 = dp3;
68+
dp3 = dp2;
69+
dp2 = dp1;
70+
dp1 = dp0 % base;
71+
dp0 = dp1;
72+
dp0 += (i - 1 == 0 && keys[i] == keys[i - 1]) ? 1 : 0;
73+
if (i - 1 <= 0 || keys[i] != keys[i - 1]) {
74+
continue;
75+
}
76+
dp0 += dp2;
77+
dp0 += (i - 2 == 0 && keys[i] == keys[i - 2]) ? 1 : 0;
78+
if (i - 2 <= 0 || keys[i] != keys[i - 2]) {
79+
continue;
80+
}
81+
dp0 += dp3;
82+
dp0 += (i - 3 == 0 && keys[i] == keys[i - 3] && (r == 7 || r == 9)) ? 1 : 0;
83+
if (i - 3 <= 0 || keys[i] != keys[i - 3] || (r != 7 && r != 9)) {
84+
continue;
85+
}
86+
dp0 += dp4;
87+
}
88+
89+
return (int) (dp0 % base);
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)