Skip to content

Commit 596ea57

Browse files
committed
Added tasks 3324-3327
1 parent 989e5b0 commit 596ea57

File tree

12 files changed

+462
-0
lines changed

12 files changed

+462
-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 #2024_10_21_Time_6_ms_(100.00%)_Space_55.6_MB_(100.00%)
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3301_3400.s3325_count_substrings_with_k_frequency_characters_i;
2+
3+
// #Medium #2024_10_21_Time_2009_ms_(100.00%)_Space_45.2_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public int numberOfSubstrings(String s, int k) {
10+
int charCount = 0;
11+
HashMap<Character, Integer> map = new HashMap<>();
12+
for (int right = 0; right < s.length(); right++) {
13+
char curr = s.charAt(right);
14+
map.put(curr, map.getOrDefault(curr, 0) + 1);
15+
PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
16+
for (char ele : map.keySet()) {
17+
queue.offer(map.get(ele));
18+
}
19+
HashMap<Character, Integer> currMap = new HashMap<>(map);
20+
for (int left = 0; left <= right; left++) {
21+
int maxEle = queue.peek();
22+
if (maxEle < k) {
23+
break;
24+
}
25+
charCount += 1;
26+
char leftChar = s.charAt(left);
27+
int leftCharCount = currMap.get(leftChar);
28+
currMap.put(leftChar, leftCharCount - 1);
29+
queue.remove(leftCharCount);
30+
if (leftCharCount > 1) {
31+
queue.offer(leftCharCount - 1);
32+
}
33+
}
34+
}
35+
return charCount;
36+
}
37+
}
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 #2024_10_21_Time_19_ms_(100.00%)_Space_69.1_MB_(100.00%)
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 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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g3301_3400.s3327_check_if_dfs_strings_are_palindromes;
2+
3+
// #Hard #2024_10_21_Time_244_ms_(100.00%)_Space_96.3_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
7+
public class Solution {
8+
private StringBuilder dfsString;
9+
10+
private int[] fillManacher(String s) {
11+
int n = s.length();
12+
s = "$" + s + "@";
13+
int[] p = new int[n + 2];
14+
int center = 0;
15+
int right = 0;
16+
for (int i = 1; i <= n; i++) {
17+
int mirror = 2 * center - i;
18+
if (i < right) {
19+
p[i] = Math.min(right - i, p[mirror]);
20+
}
21+
while (s.charAt(i + p[i]) == s.charAt(i - p[i])) {
22+
p[i]++;
23+
}
24+
if (i + p[i] > right) {
25+
center = i;
26+
right = i + p[i];
27+
}
28+
}
29+
return p;
30+
}
31+
32+
private int[] manacher() {
33+
StringBuilder temp = new StringBuilder();
34+
for (int i = 0; i < dfsString.length(); i++) {
35+
temp.append("#");
36+
temp.append(dfsString.charAt(i));
37+
}
38+
temp.append("#");
39+
return fillManacher(String.valueOf(temp));
40+
// return Arrays.copyOfRange(arr, 1, arr.length - 1);
41+
}
42+
43+
private void dfs(
44+
int node, int parent, ArrayList<ArrayList<Integer>> adj, int[][] range, String s) {
45+
int start = dfsString.length();
46+
47+
for (int neigh : adj.get(node)) {
48+
if (neigh == parent) {
49+
continue;
50+
}
51+
dfs(neigh, node, adj, range, s);
52+
}
53+
dfsString.append(s.charAt(node));
54+
int end = dfsString.length() - 1;
55+
range[node] = new int[] {start, end};
56+
}
57+
58+
public boolean[] findAnswer(int[] parent, String s) {
59+
dfsString = new StringBuilder();
60+
int n = parent.length;
61+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
62+
for (int i = 0; i < n; i++) {
63+
adj.add(new ArrayList<>());
64+
}
65+
for (int i = 0; i < n; i++) {
66+
if (parent[i] == -1) {
67+
continue;
68+
}
69+
adj.get(parent[i]).add(i);
70+
}
71+
int[][] range = new int[n][2];
72+
dfs(0, -1, adj, range, s);
73+
int[] manacherArr = manacher();
74+
boolean[] ans = new boolean[n];
75+
for (int i = 0; i < n; i++) {
76+
int[] currRange = range[i];
77+
int length = currRange[1] - currRange[0] + 1;
78+
// +2 because the string has $# in the starting and in the end
79+
int palindromeStart = 2 * currRange[0] + 2;
80+
int palindromeEnd = 2 * currRange[1] + 2;
81+
int center = (palindromeStart + palindromeEnd) / 2;
82+
// represents the palindrome length having center at center.
83+
int palindromeLength = manacherArr[center];
84+
ans[i] = palindromeLength >= length;
85+
}
86+
return ans;
87+
}
88+
}
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.

0 commit comments

Comments
 (0)