Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 411c6cd

Browse files
authored
Added tasks 3324-3327
1 parent 699a2e7 commit 411c6cd

File tree

12 files changed

+436
-0
lines changed

12 files changed

+436
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3301_3400.s3324_find_the_sequence_of_strings_appeared_on_the_screen;
2+
3+
// #Medium #String #Simulation #2024_10_22_Time_6_ms_(92.04%)_Space_55.7_MB_(44.25%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<String> stringSequence(String t) {
10+
List<String> ans = new ArrayList<>();
11+
int l = t.length();
12+
StringBuilder cur = new StringBuilder();
13+
for (int i = 0; i < l; i++) {
14+
char tCh = t.charAt(i);
15+
cur.append('a');
16+
ans.add(cur.toString());
17+
while (cur.charAt(i) != tCh) {
18+
char lastCh = cur.charAt(i);
19+
char nextCh = (char) (lastCh == 'z' ? 'a' : lastCh + 1);
20+
cur.setCharAt(i, nextCh);
21+
ans.add(cur.toString());
22+
}
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3324\. Find the Sequence of Strings Appeared on the Screen
2+
3+
Medium
4+
5+
You are given a string `target`.
6+
7+
Alice is going to type `target` on her computer using a special keyboard that has **only two** keys:
8+
9+
* Key 1 appends the character `"a"` to the string on the screen.
10+
* 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"`.
11+
12+
**Note** that initially there is an _empty_ string `""` on the screen, so she can **only** press key 1.
13+
14+
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.
15+
16+
**Example 1:**
17+
18+
**Input:** target = "abc"
19+
20+
**Output:** ["a","aa","ab","aba","abb","abc"]
21+
22+
**Explanation:**
23+
24+
The sequence of key presses done by Alice are:
25+
26+
* Press key 1, and the string on the screen becomes `"a"`.
27+
* Press key 1, and the string on the screen becomes `"aa"`.
28+
* Press key 2, and the string on the screen becomes `"ab"`.
29+
* Press key 1, and the string on the screen becomes `"aba"`.
30+
* Press key 2, and the string on the screen becomes `"abb"`.
31+
* Press key 2, and the string on the screen becomes `"abc"`.
32+
33+
**Example 2:**
34+
35+
**Input:** target = "he"
36+
37+
**Output:** ["a","b","c","d","e","f","g","h","ha","hb","hc","hd","he"]
38+
39+
**Constraints:**
40+
41+
* `1 <= target.length <= 400`
42+
* `target` consists only of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3325_count_substrings_with_k_frequency_characters_i;
2+
3+
// #Medium #String #Hash_Table #Sliding_Window #2024_10_22_Time_1_ms_(100.00%)_Space_42_MB_(98.69%)
4+
5+
public class Solution {
6+
public int numberOfSubstrings(String s, int k) {
7+
int left = 0;
8+
int result = 0;
9+
int[] count = new int[26];
10+
for (int i = 0; i < s.length(); i++) {
11+
char ch = s.charAt(i);
12+
count[ch - 'a']++;
13+
14+
while (count[ch - 'a'] == k) {
15+
result += s.length() - i;
16+
char atLeft = s.charAt(left);
17+
count[atLeft - 'a']--;
18+
left++;
19+
}
20+
}
21+
return result;
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3325\. Count Substrings With K-Frequency Characters I
2+
3+
Medium
4+
5+
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.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abacb", k = 2
10+
11+
**Output:** 4
12+
13+
**Explanation:**
14+
15+
The valid substrings are:
16+
17+
* `"aba"` (character `'a'` appears 2 times).
18+
* `"abac"` (character `'a'` appears 2 times).
19+
* `"abacb"` (character `'a'` appears 2 times).
20+
* `"bacb"` (character `'b'` appears 2 times).
21+
22+
**Example 2:**
23+
24+
**Input:** s = "abcde", k = 1
25+
26+
**Output:** 15
27+
28+
**Explanation:**
29+
30+
All substrings are valid because every character appears at least once.
31+
32+
**Constraints:**
33+
34+
* `1 <= s.length <= 3000`
35+
* `1 <= k <= s.length`
36+
* `s` consists only of lowercase English letters.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3301_3400.s3326_minimum_division_operations_to_make_array_non_decreasing;
2+
3+
// #Medium #Array #Math #Greedy #Number_Theory #2024_10_22_Time_20_ms_(97.34%)_Space_73.1_MB_(5.03%)
4+
5+
public class Solution {
6+
private static final int MAXI = 1000001;
7+
private static final int[] SIEVE = new int[MAXI];
8+
private static boolean precompute = false;
9+
10+
private static void compute() {
11+
if (precompute) {
12+
return;
13+
}
14+
for (int i = 2; i < MAXI; i++) {
15+
if (i * i > MAXI) {
16+
break;
17+
}
18+
for (int j = i * i; j < MAXI; j += i) {
19+
SIEVE[j] = Math.max(SIEVE[j], Math.max(i, j / i));
20+
}
21+
}
22+
precompute = true;
23+
}
24+
25+
public int minOperations(int[] nums) {
26+
compute();
27+
int op = 0;
28+
int n = nums.length;
29+
for (int i = n - 2; i >= 0; i--) {
30+
while (nums[i] > nums[i + 1]) {
31+
if (SIEVE[nums[i]] == 0) {
32+
return -1;
33+
}
34+
nums[i] /= SIEVE[nums[i]];
35+
op++;
36+
}
37+
if (nums[i] > nums[i + 1]) {
38+
return -1;
39+
}
40+
}
41+
return op;
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3326\. Minimum Division Operations to Make Array Non Decreasing
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
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.
8+
9+
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**.
10+
11+
Return the **minimum** number of **operations** required to make the array **non-decreasing**.
12+
13+
If it is **not** possible to make the array _non-decreasing_ using any number of operations, return `-1`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [25,7]
18+
19+
**Output:** 1
20+
21+
**Explanation:**
22+
23+
Using a single operation, 25 gets divided by 5 and `nums` becomes `[5, 7]`.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [7,7,6]
28+
29+
**Output:** \-1
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,1,1,1]
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g3301_3400.s3327_check_if_dfs_strings_are_palindromes;
2+
3+
// #Hard #Array #String #Hash_Table #Tree #Hash_Function #Depth_First_Search
4+
// #2024_10_22_Time_159_ms_(90.40%)_Space_93.9_MB_(80.80%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private final List<List<Integer>> e = new ArrayList<>();
11+
private final StringBuilder stringBuilder = new StringBuilder();
12+
private String s;
13+
private int now;
14+
private int n;
15+
private int[] l;
16+
private int[] r;
17+
private int[] p;
18+
private char[] c;
19+
20+
private void dfs(int x) {
21+
l[x] = now + 1;
22+
for (int v : e.get(x)) {
23+
dfs(v);
24+
}
25+
stringBuilder.append(s.charAt(x));
26+
r[x] = ++now;
27+
}
28+
29+
private void matcher() {
30+
c[0] = '~';
31+
c[1] = '#';
32+
for (int i = 1; i <= n; ++i) {
33+
c[2 * i + 1] = '#';
34+
c[2 * i] = stringBuilder.charAt(i - 1);
35+
}
36+
int j = 1;
37+
int mid = 0;
38+
int localR = 0;
39+
while (j <= 2 * n + 1) {
40+
if (j <= localR) {
41+
p[j] = Math.min(p[(mid << 1) - j], localR - j + 1);
42+
}
43+
while (c[j - p[j]] == c[j + p[j]]) {
44+
++p[j];
45+
}
46+
if (p[j] + j > localR) {
47+
localR = p[j] + j - 1;
48+
mid = j;
49+
}
50+
++j;
51+
}
52+
}
53+
54+
public boolean[] findAnswer(int[] parent, String s) {
55+
n = parent.length;
56+
this.s = s;
57+
for (int i = 0; i < n; ++i) {
58+
e.add(new ArrayList<>());
59+
}
60+
for (int i = 1; i < n; ++i) {
61+
e.get(parent[i]).add(i);
62+
}
63+
l = new int[n];
64+
r = new int[n];
65+
dfs(0);
66+
c = new char[2 * n + 10];
67+
p = new int[2 * n + 10];
68+
matcher();
69+
boolean[] ans = new boolean[n];
70+
for (int i = 0; i < n; ++i) {
71+
int mid = (2 * r[i] - 2 * l[i] + 1) / 2 + 2 * l[i];
72+
ans[i] = p[mid] - 1 >= r[i] - l[i] + 1;
73+
}
74+
return ans;
75+
}
76+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
3327\. Check if DFS Strings Are Palindromes
2+
3+
Hard
4+
5+
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`.
6+
7+
You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`.
8+
9+
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:
10+
11+
* Iterate over each child `y` of `x` **in increasing order of their numbers**, and call `dfs(y)`.
12+
* Add the character `s[x]` to the end of the string `dfsStr`.
13+
14+
**Note** that `dfsStr` is shared across all recursive calls of `dfs`.
15+
16+
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:
17+
18+
* Empty the string `dfsStr` and call `dfs(i)`.
19+
* If the resulting string `dfsStr` is a **palindrome**, then set `answer[i]` to `true`. Otherwise, set `answer[i]` to `false`.
20+
21+
Return the array `answer`.
22+
23+
A **palindrome** is a string that reads the same forward and backward.
24+
25+
**Example 1:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/09/01/tree1drawio.png)
28+
29+
**Input:** parent = [-1,0,0,1,1,2], s = "aababa"
30+
31+
**Output:** [true,true,false,true,true,true]
32+
33+
**Explanation:**
34+
35+
* Calling `dfs(0)` results in the string `dfsStr = "abaaba"`, which is a palindrome.
36+
* Calling `dfs(1)` results in the string `dfsStr = "aba"`, which is a palindrome.
37+
* Calling `dfs(2)` results in the string `dfsStr = "ab"`, which is **not** a palindrome.
38+
* Calling `dfs(3)` results in the string `dfsStr = "a"`, which is a palindrome.
39+
* Calling `dfs(4)` results in the string `dfsStr = "b"`, which is a palindrome.
40+
* Calling `dfs(5)` results in the string `dfsStr = "a"`, which is a palindrome.
41+
42+
**Example 2:**
43+
44+
![](https://assets.leetcode.com/uploads/2024/09/01/tree2drawio-1.png)
45+
46+
**Input:** parent = [-1,0,0,0,0], s = "aabcb"
47+
48+
**Output:** [true,true,true,true,true]
49+
50+
**Explanation:**
51+
52+
Every call on `dfs(x)` results in a palindrome string.
53+
54+
**Constraints:**
55+
56+
* `n == parent.length == s.length`
57+
* <code>1 <= n <= 10<sup>5</sup></code>
58+
* `0 <= parent[i] <= n - 1` for all `i >= 1`.
59+
* `parent[0] == -1`
60+
* `parent` represents a valid tree.
61+
* `s` consists only of lowercase English letters.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3301_3400.s3324_find_the_sequence_of_strings_appeared_on_the_screen;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void stringSequence() {
12+
assertThat(
13+
new Solution().stringSequence("abc"),
14+
equalTo(List.of("a", "aa", "ab", "aba", "abb", "abc")));
15+
}
16+
17+
@Test
18+
void stringSequence2() {
19+
assertThat(
20+
new Solution().stringSequence("he"),
21+
equalTo(
22+
List.of(
23+
"a", "b", "c", "d", "e", "f", "g", "h", "ha", "hb", "hc", "hd",
24+
"he")));
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /