Skip to content

Commit 685c0bd

Browse files
authored
Added tasks 3324-3327
1 parent 4c99f0b commit 685c0bd

File tree

5 files changed

+366
-0
lines changed
  • src/main/java/g3301_3400
    • s3324_find_the_sequence_of_strings_appeared_on_the_screen
    • s3325_count_substrings_with_k_frequency_characters_i
    • s3326_minimum_division_operations_to_make_array_non_decreasing
    • s3327_check_if_dfs_strings_are_palindromes

5 files changed

+366
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3327 |[Check if DFS Strings Are Palindromes](src/main/java/g3301_3400/s3327_check_if_dfs_strings_are_palindromes)| Hard | Array, String, Hash_Table, Tree, Hash_Function, Depth_First_Search | 159 | 90.40
1820+
| 3326 |[Minimum Division Operations to Make Array Non Decreasing](src/main/java/g3301_3400/s3326_minimum_division_operations_to_make_array_non_decreasing)| Medium | Array, Math, Greedy, Number_Theory | 20 | 97.34
1821+
| 3325 |[Count Substrings With K-Frequency Characters I](src/main/java/g3301_3400/s3325_count_substrings_with_k_frequency_characters_i)| Medium | String, Hash_Table, Sliding_Window | 1 | 100.00
1822+
| 3324 |[Find the Sequence of Strings Appeared on the Screen](src/main/java/g3301_3400/s3324_find_the_sequence_of_strings_appeared_on_the_screen)| Medium | String, Simulation | 6 | 92.04
18191823
| 3321 |[Find X-Sum of All K-Long Subarrays II](src/main/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii)| Hard | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 410 | 94.03
18201824
| 3320 |[Count The Number of Winning Sequences](src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences)| Hard | String, Dynamic_Programming | 43 | 99.76
18211825
| 3319 |[K-th Largest Perfect Subtree Size in Binary Tree](src/main/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree)| Medium | Sorting, Tree, Binary_Tree, Depth_First_Search | 10 | 87.48
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+
## 3324\. Find the Sequence of Strings Appeared on the Screen
5+
6+
Medium
7+
8+
You are given a string `target`.
9+
10+
Alice is going to type `target` on her computer using a special keyboard that has **only two** keys:
11+
12+
* Key 1 appends the character `"a"` to the string on the screen.
13+
* Key 2 changes the **last** character of the string on the screen to its **next** character in the English alphabet. For example, `"c"` changes to `"d"` and `"z"` changes to `"a"`.
14+
15+
**Note** that initially there is an _empty_ string `""` on the screen, so she can **only** press key 1.
16+
17+
Return a list of _all_ strings that appear on the screen as Alice types `target`, in the order they appear, using the **minimum** key presses.
18+
19+
**Example 1:**
20+
21+
**Input:** target = "abc"
22+
23+
**Output:** ["a","aa","ab","aba","abb","abc"]
24+
25+
**Explanation:**
26+
27+
The sequence of key presses done by Alice are:
28+
29+
* Press key 1, and the string on the screen becomes `"a"`.
30+
* Press key 1, and the string on the screen becomes `"aa"`.
31+
* Press key 2, and the string on the screen becomes `"ab"`.
32+
* Press key 1, and the string on the screen becomes `"aba"`.
33+
* Press key 2, and the string on the screen becomes `"abb"`.
34+
* Press key 2, and the string on the screen becomes `"abc"`.
35+
36+
**Example 2:**
37+
38+
**Input:** target = "he"
39+
40+
**Output:** ["a","b","c","d","e","f","g","h","ha","hb","hc","hd","he"]
41+
42+
**Constraints:**
43+
44+
* `1 <= target.length <= 400`
45+
* `target` consists only of lowercase English letters.
46+
47+
## Solution
48+
49+
```java
50+
import java.util.ArrayList;
51+
import java.util.List;
52+
53+
public class Solution {
54+
public List<String> stringSequence(String t) {
55+
List<String> ans = new ArrayList<>();
56+
int l = t.length();
57+
StringBuilder cur = new StringBuilder();
58+
for (int i = 0; i < l; i++) {
59+
char tCh = t.charAt(i);
60+
cur.append('a');
61+
ans.add(cur.toString());
62+
while (cur.charAt(i) != tCh) {
63+
char lastCh = cur.charAt(i);
64+
char nextCh = (char) (lastCh == 'z' ? 'a' : lastCh + 1);
65+
cur.setCharAt(i, nextCh);
66+
ans.add(cur.toString());
67+
}
68+
}
69+
return ans;
70+
}
71+
}
72+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 3325\. Count Substrings With K-Frequency Characters I
5+
6+
Medium
7+
8+
Given a string `s` and an integer `k`, return the total number of substrings of `s` where **at least one** character appears **at least** `k` times.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "abacb", k = 2
13+
14+
**Output:** 4
15+
16+
**Explanation:**
17+
18+
The valid substrings are:
19+
20+
* `"aba"` (character `'a'` appears 2 times).
21+
* `"abac"` (character `'a'` appears 2 times).
22+
* `"abacb"` (character `'a'` appears 2 times).
23+
* `"bacb"` (character `'b'` appears 2 times).
24+
25+
**Example 2:**
26+
27+
**Input:** s = "abcde", k = 1
28+
29+
**Output:** 15
30+
31+
**Explanation:**
32+
33+
All substrings are valid because every character appears at least once.
34+
35+
**Constraints:**
36+
37+
* `1 <= s.length <= 3000`
38+
* `1 <= k <= s.length`
39+
* `s` consists only of lowercase English letters.
40+
41+
## Solution
42+
43+
```java
44+
public class Solution {
45+
public int numberOfSubstrings(String s, int k) {
46+
int left = 0;
47+
int result = 0;
48+
int[] count = new int[26];
49+
for (int i = 0; i < s.length(); i++) {
50+
char ch = s.charAt(i);
51+
count[ch - 'a']++;
52+
53+
while (count[ch - 'a'] == k) {
54+
result += s.length() - i;
55+
char atLeft = s.charAt(left);
56+
count[atLeft - 'a']--;
57+
left++;
58+
}
59+
}
60+
return result;
61+
}
62+
}
63+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
## 3326\. Minimum Division Operations to Make Array Non Decreasing
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
Any **positive** divisor of a natural number `x` that is **strictly less** than `x` is called a **proper divisor** of `x`. For example, 2 is a _proper divisor_ of 4, while 6 is not a _proper divisor_ of 6.
11+
12+
You are allowed to perform an **operation** any number of times on `nums`, where in each **operation** you select any _one_ element from `nums` and divide it by its **greatest** **proper divisor**.
13+
14+
Return the **minimum** number of **operations** required to make the array **non-decreasing**.
15+
16+
If it is **not** possible to make the array _non-decreasing_ using any number of operations, return `-1`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [25,7]
21+
22+
**Output:** 1
23+
24+
**Explanation:**
25+
26+
Using a single operation, 25 gets divided by 5 and `nums` becomes `[5, 7]`.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [7,7,6]
31+
32+
**Output:** \-1
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [1,1,1,1]
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
43+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
44+
45+
## Solution
46+
47+
```java
48+
public class Solution {
49+
private static final int MAXI = 1000001;
50+
private static final int[] SIEVE = new int[MAXI];
51+
private static boolean precompute = false;
52+
53+
private static void compute() {
54+
if (precompute) {
55+
return;
56+
}
57+
for (int i = 2; i < MAXI; i++) {
58+
if (i * i > MAXI) {
59+
break;
60+
}
61+
for (int j = i * i; j < MAXI; j += i) {
62+
SIEVE[j] = Math.max(SIEVE[j], Math.max(i, j / i));
63+
}
64+
}
65+
precompute = true;
66+
}
67+
68+
public int minOperations(int[] nums) {
69+
compute();
70+
int op = 0;
71+
int n = nums.length;
72+
for (int i = n - 2; i >= 0; i--) {
73+
while (nums[i] > nums[i + 1]) {
74+
if (SIEVE[nums[i]] == 0) {
75+
return -1;
76+
}
77+
nums[i] /= SIEVE[nums[i]];
78+
op++;
79+
}
80+
if (nums[i] > nums[i + 1]) {
81+
return -1;
82+
}
83+
}
84+
return op;
85+
}
86+
}
87+
```
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
## 3327\. Check if DFS Strings Are Palindromes
5+
6+
Hard
7+
8+
You are given a tree rooted at node 0, consisting 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+
Consider an empty string `dfsStr`, and define a recursive function `dfs(int x)` that takes a node `x` as a parameter and performs the following steps in order:
13+
14+
* Iterate over each child `y` of `x` **in increasing order of their numbers**, and call `dfs(y)`.
15+
* Add the character `s[x]` to the end of the string `dfsStr`.
16+
17+
**Note** that `dfsStr` is shared across all recursive calls of `dfs`.
18+
19+
You need to find a boolean array `answer` of size `n`, where for each index `i` from `0` to `n - 1`, you do the following:
20+
21+
* Empty the string `dfsStr` and call `dfs(i)`.
22+
* If the resulting string `dfsStr` is a **palindrome**, then set `answer[i]` to `true`. Otherwise, set `answer[i]` to `false`.
23+
24+
Return the array `answer`.
25+
26+
A **palindrome** is a string that reads the same forward and backward.
27+
28+
**Example 1:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/09/01/tree1drawio.png)
31+
32+
**Input:** parent = [-1,0,0,1,1,2], s = "aababa"
33+
34+
**Output:** [true,true,false,true,true,true]
35+
36+
**Explanation:**
37+
38+
* Calling `dfs(0)` results in the string `dfsStr = "abaaba"`, which is a palindrome.
39+
* Calling `dfs(1)` results in the string `dfsStr = "aba"`, which is a palindrome.
40+
* Calling `dfs(2)` results in the string `dfsStr = "ab"`, which is **not** a palindrome.
41+
* Calling `dfs(3)` results in the string `dfsStr = "a"`, which is a palindrome.
42+
* Calling `dfs(4)` results in the string `dfsStr = "b"`, which is a palindrome.
43+
* Calling `dfs(5)` results in the string `dfsStr = "a"`, which is a palindrome.
44+
45+
**Example 2:**
46+
47+
![](https://assets.leetcode.com/uploads/2024/09/01/tree2drawio-1.png)
48+
49+
**Input:** parent = [-1,0,0,0,0], s = "aabcb"
50+
51+
**Output:** [true,true,true,true,true]
52+
53+
**Explanation:**
54+
55+
Every call on `dfs(x)` results in a palindrome string.
56+
57+
**Constraints:**
58+
59+
* `n == parent.length == s.length`
60+
* <code>1 <= n <= 10<sup>5</sup></code>
61+
* `0 <= parent[i] <= n - 1` for all `i >= 1`.
62+
* `parent[0] == -1`
63+
* `parent` represents a valid tree.
64+
* `s` consists only of lowercase English letters.
65+
66+
## Solution
67+
68+
```java
69+
import java.util.ArrayList;
70+
import java.util.List;
71+
72+
public class Solution {
73+
private final List<List<Integer>> e = new ArrayList<>();
74+
private final StringBuilder stringBuilder = new StringBuilder();
75+
private String s;
76+
private int now;
77+
private int n;
78+
private int[] l;
79+
private int[] r;
80+
private int[] p;
81+
private char[] c;
82+
83+
private void dfs(int x) {
84+
l[x] = now + 1;
85+
for (int v : e.get(x)) {
86+
dfs(v);
87+
}
88+
stringBuilder.append(s.charAt(x));
89+
r[x] = ++now;
90+
}
91+
92+
private void matcher() {
93+
c[0] = '~';
94+
c[1] = '#';
95+
for (int i = 1; i <= n; ++i) {
96+
c[2 * i + 1] = '#';
97+
c[2 * i] = stringBuilder.charAt(i - 1);
98+
}
99+
int j = 1;
100+
int mid = 0;
101+
int localR = 0;
102+
while (j <= 2 * n + 1) {
103+
if (j <= localR) {
104+
p[j] = Math.min(p[(mid << 1) - j], localR - j + 1);
105+
}
106+
while (c[j - p[j]] == c[j + p[j]]) {
107+
++p[j];
108+
}
109+
if (p[j] + j > localR) {
110+
localR = p[j] + j - 1;
111+
mid = j;
112+
}
113+
++j;
114+
}
115+
}
116+
117+
public boolean[] findAnswer(int[] parent, String s) {
118+
n = parent.length;
119+
this.s = s;
120+
for (int i = 0; i < n; ++i) {
121+
e.add(new ArrayList<>());
122+
}
123+
for (int i = 1; i < n; ++i) {
124+
e.get(parent[i]).add(i);
125+
}
126+
l = new int[n];
127+
r = new int[n];
128+
dfs(0);
129+
c = new char[2 * n + 10];
130+
p = new int[2 * n + 10];
131+
matcher();
132+
boolean[] ans = new boolean[n];
133+
for (int i = 0; i < n; ++i) {
134+
int mid = (2 * r[i] - 2 * l[i] + 1) / 2 + 2 * l[i];
135+
ans[i] = p[mid] - 1 >= r[i] - l[i] + 1;
136+
}
137+
return ans;
138+
}
139+
}
140+
```

0 commit comments

Comments
 (0)