Skip to content

Commit bfbe531

Browse files
authored
Added tasks 3324-3337
1 parent b84ccda commit bfbe531

File tree

14 files changed

+1310
-107
lines changed
  • src/main/kotlin
    • g2601_2700/s2622_cache_with_time_limit
    • 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
      • 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

14 files changed

+1310
-107
lines changed

README.md

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

src/main/kotlin/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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
50+
class Solution {
51+
fun stringSequence(target: String): List<String> {
52+
val ans: MutableList<String> = ArrayList<String>()
53+
val l = target.length
54+
val cur = StringBuilder()
55+
for (i in 0 until l) {
56+
val tCh = target[i]
57+
cur.append('a')
58+
ans.add(cur.toString())
59+
while (cur[i] != tCh) {
60+
val lastCh = cur[i]
61+
val nextCh = (if (lastCh == 'z') 'a'.code else lastCh.code + 1).toChar()
62+
cur.setCharAt(i, nextCh)
63+
ans.add(cur.toString())
64+
}
65+
}
66+
return ans
67+
}
68+
}
69+
```
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-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
44+
class Solution {
45+
fun numberOfSubstrings(s: String, k: Int): Int {
46+
var left = 0
47+
var result = 0
48+
val count = IntArray(26)
49+
for (i in 0 until s.length) {
50+
val ch = s[i]
51+
count[ch.code - 'a'.code]++
52+
53+
while (count[ch.code - 'a'.code] == k) {
54+
result += s.length - i
55+
val atLeft = s[left]
56+
count[atLeft.code - 'a'.code]--
57+
left++
58+
}
59+
}
60+
return result
61+
}
62+
}
63+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
48+
import kotlin.math.max
49+
50+
class Solution {
51+
fun minOperations(nums: IntArray): Int {
52+
compute()
53+
var op = 0
54+
val n = nums.size
55+
for (i in n - 2 downTo 0) {
56+
while (nums[i] > nums[i + 1]) {
57+
if (SIEVE[nums[i]] == 0) {
58+
return -1
59+
}
60+
nums[i] /= SIEVE[nums[i]]
61+
op++
62+
}
63+
if (nums[i] > nums[i + 1]) {
64+
return -1
65+
}
66+
}
67+
return op
68+
}
69+
70+
companion object {
71+
private const val MAXI = 1000001
72+
private val SIEVE = IntArray(MAXI)
73+
private var precompute = false
74+
75+
private fun compute() {
76+
if (precompute) {
77+
return
78+
}
79+
for (i in 2 until MAXI) {
80+
if (i * i > MAXI) {
81+
break
82+
}
83+
var j = i * i
84+
while (j < MAXI) {
85+
SIEVE[j] =
86+
max(SIEVE[j], max(i, (j / i)))
87+
j += i
88+
}
89+
}
90+
precompute = true
91+
}
92+
}
93+
}
94+
```
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
69+
import kotlin.math.min
70+
71+
class Solution {
72+
private val e: MutableList<MutableList<Int?>?> = ArrayList<MutableList<Int?>?>()
73+
private val stringBuilder = StringBuilder()
74+
private var s: String? = null
75+
private var now = 0
76+
private var n = 0
77+
private lateinit var l: IntArray
78+
private lateinit var r: IntArray
79+
private lateinit var p: IntArray
80+
private lateinit var c: CharArray
81+
82+
private fun dfs(x: Int) {
83+
l[x] = now + 1
84+
for (v in e[x]!!) {
85+
dfs(v!!)
86+
}
87+
stringBuilder.append(s!![x])
88+
r[x] = ++now
89+
}
90+
91+
private fun matcher() {
92+
c[0] = '~'
93+
c[1] = '#'
94+
for (i in 1..n) {
95+
c[2 * i + 1] = '#'
96+
c[2 * i] = stringBuilder[i - 1]
97+
}
98+
var j = 1
99+
var mid = 0
100+
var localR = 0
101+
while (j <= 2 * n + 1) {
102+
if (j <= localR) {
103+
p[j] = min(p[(mid shl 1) - j], (localR - j + 1))
104+
}
105+
while (c[j - p[j]] == c[j + p[j]]) {
106+
++p[j]
107+
}
108+
if (p[j] + j > localR) {
109+
localR = p[j] + j - 1
110+
mid = j
111+
}
112+
++j
113+
}
114+
}
115+
116+
fun findAnswer(parent: IntArray, s: String): BooleanArray {
117+
n = parent.size
118+
this.s = s
119+
for (i in 0 until n) {
120+
e.add(ArrayList<Int?>())
121+
}
122+
for (i in 1 until n) {
123+
e[parent[i]]!!.add(i)
124+
}
125+
l = IntArray(n)
126+
r = IntArray(n)
127+
dfs(0)
128+
c = CharArray(2 * n + 10)
129+
p = IntArray(2 * n + 10)
130+
matcher()
131+
val ans = BooleanArray(n)
132+
for (i in 0 until n) {
133+
val mid = (2 * r[i] - 2 * l[i] + 1) / 2 + 2 * l[i]
134+
ans[i] = p[mid] - 1 >= r[i] - l[i] + 1
135+
}
136+
return ans
137+
}
138+
}
139+
```

0 commit comments

Comments
 (0)