Skip to content

Commit 9ecc6a1

Browse files
authored
Added tasks 3330-3337
1 parent 685c0bd commit 9ecc6a1

File tree

10 files changed

+930
-107
lines changed
  • src/main/java
    • g2601_2700/s2622_cache_with_time_limit
    • g3301_3400
      • s3330_find_the_original_typed_string_i
      • s3331_find_subtree_sizes_after_changes
      • s3332_maximum_points_tourist_can_earn
      • s3333_find_the_original_typed_string_ii
      • s3334_find_the_maximum_factor_score_of_array
      • s3335_total_characters_in_string_after_transformations_i
      • s3336_find_the_number_of_subsequences_with_equal_gcd
      • s3337_total_characters_in_string_after_transformations_ii

10 files changed

+930
-107
lines changed

README.md

Lines changed: 114 additions & 106 deletions
Large diffs are not rendered by default.

src/main/java/g2601_2700/s2622_cache_with_time_limit/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ At t=250, count() returns 0 because the cache is empty.
6666

6767
```typescript
6868
class TimeLimitedCache {
69-
private keyMap: Map<number, any>
69+
private readonly keyMap: Map<number, any>
7070
constructor() {
7171
this.keyMap = new Map<number, any>()
7272
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3330\. Find the Original Typed String I
5+
6+
Easy
7+
8+
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times.
9+
10+
Although Alice tried to focus on her typing, she is aware that she may still have done this **at most** _once_.
11+
12+
You are given a string `word`, which represents the **final** output displayed on Alice's screen.
13+
14+
Return the total number of _possible_ original strings that Alice _might_ have intended to type.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "abbcccc"
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
The possible strings are: `"abbcccc"`, `"abbccc"`, `"abbcc"`, `"abbc"`, and `"abcccc"`.
25+
26+
**Example 2:**
27+
28+
**Input:** word = "abcd"
29+
30+
**Output:** 1
31+
32+
**Explanation:**
33+
34+
The only possible string is `"abcd"`.
35+
36+
**Example 3:**
37+
38+
**Input:** word = "aaaa"
39+
40+
**Output:** 4
41+
42+
**Constraints:**
43+
44+
* `1 <= word.length <= 100`
45+
* `word` consists only of lowercase English letters.
46+
47+
## Solution
48+
49+
```java
50+
public class Solution {
51+
public int possibleStringCount(String word) {
52+
int n = word.length();
53+
int count = 1;
54+
char pre = word.charAt(0);
55+
int temp = 0;
56+
for (int i = 1; i < n; i++) {
57+
char ch = word.charAt(i);
58+
if (ch == pre) {
59+
temp++;
60+
} else {
61+
if (temp >= 1) {
62+
count += temp;
63+
}
64+
temp = 0;
65+
pre = ch;
66+
}
67+
}
68+
if (temp >= 1) {
69+
count += temp;
70+
}
71+
return count;
72+
}
73+
}
74+
```
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3331\. Find Subtree Sizes After Changes
5+
6+
Medium
7+
8+
You are given a tree rooted at node 0 that consists of `n` nodes numbered from `0` to `n - 1`. The tree is represented by an array `parent` of size `n`, where `parent[i]` is the parent of node `i`. Since node 0 is the root, `parent[0] == -1`.
9+
10+
You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`.
11+
12+
We make the following changes on the tree **one** time **simultaneously** for all nodes `x` from `1` to `n - 1`:
13+
14+
* Find the **closest** node `y` to node `x` such that `y` is an ancestor of `x`, and `s[x] == s[y]`.
15+
* If node `y` does not exist, do nothing.
16+
* Otherwise, **remove** the edge between `x` and its current parent and make node `y` the new parent of `x` by adding an edge between them.
17+
18+
Return an array `answer` of size `n` where `answer[i]` is the **size** of the subtree rooted at node `i` in the **final** tree.
19+
20+
A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.
21+
22+
**Example 1:**
23+
24+
**Input:** parent = [-1,0,0,1,1,1], s = "abaabc"
25+
26+
**Output:** [6,3,1,1,1,1]
27+
28+
**Explanation:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/08/15/graphex1drawio.png)
31+
32+
The parent of node 3 will change from node 1 to node 0.
33+
34+
**Example 2:**
35+
36+
**Input:** parent = [-1,0,4,0,1], s = "abbba"
37+
38+
**Output:** [5,2,1,1,1]
39+
40+
**Explanation:**
41+
42+
![](https://assets.leetcode.com/uploads/2024/08/20/exgraph2drawio.png)
43+
44+
The following changes will happen at the same time:
45+
46+
* The parent of node 4 will change from node 1 to node 0.
47+
* The parent of node 2 will change from node 4 to node 1.
48+
49+
**Constraints:**
50+
51+
* `n == parent.length == s.length`
52+
* <code>1 <= n <= 10<sup>5</sup></code>
53+
* `0 <= parent[i] <= n - 1` for all `i >= 1`.
54+
* `parent[0] == -1`
55+
* `parent` represents a valid tree.
56+
* `s` consists only of lowercase English letters.
57+
58+
## Solution
59+
60+
```java
61+
import java.util.ArrayList;
62+
import java.util.HashMap;
63+
64+
public class Solution {
65+
private int[] finalAns;
66+
67+
public int[] findSubtreeSizes(int[] parent, String s) {
68+
int n = parent.length;
69+
char[] arr = s.toCharArray();
70+
int[] newParent = new int[n];
71+
finalAns = new int[n];
72+
HashMap<Integer, ArrayList<Integer>> tree = new HashMap<>();
73+
74+
for (int i = 1; i < n; i++) {
75+
int parentNode = parent[i];
76+
newParent[i] = parentNode;
77+
while (parentNode != -1) {
78+
if (arr[parentNode] == arr[i]) {
79+
newParent[i] = parentNode;
80+
break;
81+
}
82+
parentNode = parent[parentNode];
83+
}
84+
}
85+
86+
for (int i = 1; i < n; i++) {
87+
if (!tree.containsKey(newParent[i])) {
88+
tree.put(newParent[i], new ArrayList<>());
89+
}
90+
91+
tree.get(newParent[i]).add(i);
92+
}
93+
94+
findNodes(0, tree);
95+
return finalAns;
96+
}
97+
98+
private int findNodes(int parent, HashMap<Integer, ArrayList<Integer>> tree) {
99+
int count = 1;
100+
if (tree.containsKey(parent)) {
101+
for (int i : tree.get(parent)) {
102+
count += findNodes(i, tree);
103+
}
104+
}
105+
finalAns[parent] = count;
106+
return count;
107+
}
108+
}
109+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3332\. Maximum Points Tourist Can Earn
5+
6+
Medium
7+
8+
You are given two integers, `n` and `k`, along with two 2D integer arrays, `stayScore` and `travelScore`.
9+
10+
A tourist is visiting a country with `n` cities, where each city is **directly** connected to every other city. The tourist's journey consists of **exactly** `k` **0-indexed** days, and they can choose **any** city as their starting point.
11+
12+
Each day, the tourist has two choices:
13+
14+
* **Stay in the current city**: If the tourist stays in their current city `curr` during day `i`, they will earn `stayScore[i][curr]` points.
15+
* **Move to another city**: If the tourist moves from their current city `curr` to city `dest`, they will earn `travelScore[curr][dest]` points.
16+
17+
Return the **maximum** possible points the tourist can earn.
18+
19+
**Example 1:**
20+
21+
**Input:** n = 2, k = 1, stayScore = \[\[2,3]], travelScore = \[\[0,2],[1,0]]
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
The tourist earns the maximum number of points by starting in city 1 and staying in that city.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 3, k = 2, stayScore = \[\[3,4,2],[2,1,2]], travelScore = \[\[0,2,1],[2,0,4],[3,2,0]]
32+
33+
**Output:** 8
34+
35+
**Explanation:**
36+
37+
The tourist earns the maximum number of points by starting in city 1, staying in that city on day 0, and traveling to city 2 on day 1.
38+
39+
**Constraints:**
40+
41+
* `1 <= n <= 200`
42+
* `1 <= k <= 200`
43+
* `n == travelScore.length == travelScore[i].length == stayScore[i].length`
44+
* `k == stayScore.length`
45+
* `1 <= stayScore[i][j] <= 100`
46+
* `0 <= travelScore[i][j] <= 100`
47+
* `travelScore[i][i] == 0`
48+
49+
## Solution
50+
51+
```java
52+
public class Solution {
53+
public int maxScore(int n, int k, int[][] stayScores, int[][] travelScores) {
54+
// dp[day][city]
55+
int[][] dp = new int[k + 1][n];
56+
int result = 0;
57+
for (int day = k - 1; day >= 0; day--) {
58+
for (int city = 0; city < n; city++) {
59+
int stayScore = stayScores[day][city] + dp[day + 1][city];
60+
int travelScore = 0;
61+
for (int nextCity = 0; nextCity < n; nextCity++) {
62+
int nextScore = travelScores[city][nextCity] + dp[day + 1][nextCity];
63+
travelScore = Math.max(nextScore, travelScore);
64+
}
65+
dp[day][city] = Math.max(stayScore, travelScore);
66+
result = Math.max(dp[day][city], result);
67+
}
68+
}
69+
return result;
70+
}
71+
}
72+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3333\. Find the Original Typed String II
5+
6+
Hard
7+
8+
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times.
9+
10+
You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`.
11+
12+
Return the total number of _possible_ original strings that Alice _might_ have intended to type, if she was trying to type a string of size **at least** `k`.
13+
14+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "aabbccdd", k = 7
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
The possible strings are: `"aabbccdd"`, `"aabbccd"`, `"aabbcdd"`, `"aabccdd"`, and `"abbccdd"`.
25+
26+
**Example 2:**
27+
28+
**Input:** word = "aabbccdd", k = 8
29+
30+
**Output:** 1
31+
32+
**Explanation:**
33+
34+
The only possible string is `"aabbccdd"`.
35+
36+
**Example 3:**
37+
38+
**Input:** word = "aaabbb", k = 3
39+
40+
**Output:** 8
41+
42+
**Constraints:**
43+
44+
* <code>1 <= word.length <= 5 * 10<sup>5</sup></code>
45+
* `word` consists only of lowercase English letters.
46+
* `1 <= k <= 2000`
47+
48+
## Solution
49+
50+
```java
51+
import java.util.ArrayList;
52+
import java.util.List;
53+
54+
public class Solution {
55+
private static final long MOD = (long) 1e9 + 7;
56+
57+
public int possibleStringCount(String word, int k) {
58+
List<Integer> list = new ArrayList<>();
59+
int n = word.length();
60+
int i = 0;
61+
while (i < n) {
62+
int j = i + 1;
63+
while (j < n && word.charAt(j) == word.charAt(j - 1)) {
64+
j++;
65+
}
66+
list.add(j - i);
67+
i = j;
68+
}
69+
int m = list.size();
70+
long[] power = new long[m];
71+
power[m - 1] = list.get(m - 1);
72+
for (i = m - 2; i >= 0; i--) {
73+
power[i] = (power[i + 1] * list.get(i)) % MOD;
74+
}
75+
if (m >= k) {
76+
return (int) power[0];
77+
}
78+
long[][] dp = new long[m][k - m + 1];
79+
for (i = 0; i < k - m + 1; i++) {
80+
if (list.get(m - 1) + i + m > k) {
81+
dp[m - 1][i] = list.get(m - 1) - (long) (k - m - i);
82+
}
83+
}
84+
for (i = m - 2; i >= 0; i--) {
85+
long sum = (dp[i + 1][k - m] * list.get(i)) % MOD;
86+
for (int j = k - m; j >= 0; j--) {
87+
sum += dp[i + 1][j];
88+
if (j + list.get(i) > k - m) {
89+
sum = (sum - dp[i + 1][k - m] + MOD) % MOD;
90+
} else {
91+
sum = (sum - dp[i + 1][j + list.get(i)] + MOD) % MOD;
92+
}
93+
dp[i][j] = sum;
94+
}
95+
}
96+
return (int) dp[0][0];
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)