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 f180065

Browse files
authored
Added tasks 3612-3615
1 parent 2b12fe6 commit f180065

File tree

12 files changed

+634
-0
lines changed

12 files changed

+634
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3601_3700.s3612_process_string_with_special_operations_i
2+
3+
// #Medium #String #Simulation #2025_07_14_Time_5_ms_(100.00%)_Space_54.27_MB_(100.00%)
4+
5+
class Solution {
6+
fun processStr(s: String): String {
7+
val res = StringBuilder()
8+
for (c in s.toCharArray()) {
9+
if (c != '*' && c != '#' && c != '%') {
10+
res.append(c)
11+
} else if (c == '#') {
12+
res.append(res)
13+
} else if (c == '%') {
14+
res.reverse()
15+
} else {
16+
if (res.isNotEmpty()) {
17+
res.deleteCharAt(res.length - 1)
18+
}
19+
}
20+
}
21+
return res.toString()
22+
}
23+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
3612\. Process String with Special Operations I
2+
3+
Medium
4+
5+
You are given a string `s` consisting of lowercase English letters and the special characters: `*`, `#`, and `%`.
6+
7+
Build a new string `result` by processing `s` according to the following rules from left to right:
8+
9+
* If the letter is a **lowercase** English letter append it to `result`.
10+
* A `'*'` **removes** the last character from `result`, if it exists.
11+
* A `'#'` **duplicates** the current `result` and **appends** it to itself.
12+
* A `'%'` **reverses** the current `result`.
13+
14+
Return the final string `result` after processing all characters in `s`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "a#b%\*"
19+
20+
**Output:** "ba"
21+
22+
**Explanation:**
23+
24+
`i`
25+
26+
`s[i]`
27+
28+
Operation
29+
30+
Current `result`
31+
32+
0
33+
34+
`'a'`
35+
36+
Append `'a'`
37+
38+
`"a"`
39+
40+
1
41+
42+
`'#'`
43+
44+
Duplicate `result`
45+
46+
`"aa"`
47+
48+
2
49+
50+
`'b'`
51+
52+
Append `'b'`
53+
54+
`"aab"`
55+
56+
3
57+
58+
`'%'`
59+
60+
Reverse `result`
61+
62+
`"baa"`
63+
64+
4
65+
66+
`'*'`
67+
68+
Remove the last character
69+
70+
`"ba"`
71+
72+
Thus, the final `result` is `"ba"`.
73+
74+
**Example 2:**
75+
76+
**Input:** s = "z\*#"
77+
78+
**Output:** ""
79+
80+
**Explanation:**
81+
82+
`i`
83+
84+
`s[i]`
85+
86+
Operation
87+
88+
Current `result`
89+
90+
0
91+
92+
`'z'`
93+
94+
Append `'z'`
95+
96+
`"z"`
97+
98+
1
99+
100+
`'*'`
101+
102+
Remove the last character
103+
104+
`""`
105+
106+
2
107+
108+
`'#'`
109+
110+
Duplicate the string
111+
112+
`""`
113+
114+
Thus, the final `result` is `""`.
115+
116+
**Constraints:**
117+
118+
* `1 <= s.length <= 20`
119+
* `s` consists of only lowercase English letters and special characters `*`, `#`, and `%`.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g3601_3700.s3613_minimize_maximum_component_cost
2+
3+
// #Medium #Binary_Search #Graph #Union_Find #Sort
4+
// #2025_07_14_Time_34_ms_(100.00%)_Space_134.64_MB_(44.44%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun minCost(ui: Int, pl: Array<IntArray>, zx: Int): Int {
10+
var rt = 0
11+
var gh = 0
12+
var i = 0
13+
while (i < pl.size) {
14+
gh = max(gh, pl[i][2])
15+
i++
16+
}
17+
while (rt < gh) {
18+
val ty = rt + (gh - rt) / 2
19+
if (dfgh(ui, pl, ty, zx)) {
20+
gh = ty
21+
} else {
22+
rt = ty + 1
23+
}
24+
}
25+
return rt
26+
}
27+
28+
private fun dfgh(ui: Int, pl: Array<IntArray>, jk: Int, zx: Int): Boolean {
29+
val wt = IntArray(ui)
30+
var i = 0
31+
while (i < ui) {
32+
wt[i] = i
33+
i++
34+
}
35+
var er = ui
36+
i = 0
37+
while (i < pl.size) {
38+
val df = pl[i]
39+
if (df[2] > jk) {
40+
i++
41+
continue
42+
}
43+
val u = cvb(wt, df[0])
44+
val v = cvb(wt, df[1])
45+
if (u != v) {
46+
wt[u] = v
47+
er--
48+
}
49+
i++
50+
}
51+
return er <= zx
52+
}
53+
54+
private fun cvb(wt: IntArray, i: Int): Int {
55+
var i = i
56+
while (wt[i] != i) {
57+
wt[i] = wt[wt[i]]
58+
i = wt[i]
59+
}
60+
return i
61+
}
62+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3613\. Minimize Maximum Component Cost
2+
3+
Medium
4+
5+
You are given an undirected connected graph with `n` nodes labeled from 0 to `n - 1` and a 2D integer array `edges` where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> denotes an undirected edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>, and an integer `k`.
6+
7+
You are allowed to remove any number of edges from the graph such that the resulting graph has **at most** `k` connected components.
8+
9+
The **cost** of a component is defined as the **maximum** edge weight in that component. If a component has no edges, its cost is 0.
10+
11+
Return the **minimum** possible value of the **maximum** cost among all components **after such removals**.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2025/04/19/minimizemaximumm.jpg)
22+
23+
* Remove the edge between nodes 3 and 4 (weight 6).
24+
* The resulting components have costs of 0 and 4, so the overall maximum cost is 4.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 4, edges = [[0,1,5],[1,2,5],[2,3,5]], k = 1
29+
30+
**Output:** 5
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2025/04/19/minmax2.jpg)
35+
36+
* No edge can be removed, since allowing only one component (`k = 1`) requires the graph to stay fully connected.
37+
* That single component’s cost equals its largest edge weight, which is 5.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
42+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
43+
* `edges[i].length == 3`
44+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
45+
* <code>1 <= w<sub>i</sub> <= 10<sup>6</sup></code>
46+
* `1 <= k <= n`
47+
* The input graph is connected.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3601_3700.s3614_process_string_with_special_operations_ii
2+
3+
// #Hard #String #Simulation #2025_07_14_Time_37_ms_(100.00%)_Space_50.21_MB_(100.00%)
4+
5+
class Solution {
6+
fun processStr(s: String, k: Long): Char {
7+
var k = k
8+
var len: Long = 0
9+
for (c in s.toCharArray()) {
10+
if (Character.isLowerCase(c)) {
11+
len++
12+
} else if (c == '*' && len > 0) {
13+
len--
14+
} else if (c == '#') {
15+
len *= 2
16+
}
17+
}
18+
if (k >= len) {
19+
return '.'
20+
}
21+
for (i in s.length - 1 downTo 0) {
22+
val c = s[i]
23+
if (Character.isLowerCase(c)) {
24+
if (k == len - 1) {
25+
return c
26+
}
27+
len--
28+
} else if (c == '*') {
29+
len++
30+
} else if (c == '#') {
31+
len /= 2
32+
if (k >= len) {
33+
k -= len
34+
}
35+
} else if (c == '%') {
36+
k = len - 1 - k
37+
}
38+
}
39+
return '.'
40+
}
41+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
3614\. Process String with Special Operations II
2+
3+
Hard
4+
5+
You are given a string `s` consisting of lowercase English letters and the special characters: `'*'`, `'#'`, and `'%'`.
6+
7+
You are also given an integer `k`.
8+
9+
Build a new string `result` by processing `s` according to the following rules from left to right:
10+
11+
* If the letter is a **lowercase** English letter append it to `result`.
12+
* A `'*'` **removes** the last character from `result`, if it exists.
13+
* A `'#'` **duplicates** the current `result` and **appends** it to itself.
14+
* A `'%'` **reverses** the current `result`.
15+
16+
Return the <code>k<sup>th</sup></code> character of the final string `result`. If `k` is out of the bounds of `result`, return `'.'`.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "a#b%\*", k = 1
21+
22+
**Output:** "a"
23+
24+
**Explanation:**
25+
26+
| i | s[i] | Operation | Current `result` |
27+
|---|-------|----------------------------|------------------|
28+
| 0 | `'a'` | Append `'a'` | `"a"` |
29+
| 1 | `'#'` | Duplicate `result` | `"aa"` |
30+
| 2 | `'b'` | Append `'b'` | `"aab"` |
31+
| 3 | `'%'` | Reverse `result` | `"baa"` |
32+
| 4 | `'*'` | Remove the last character | `"ba"` |
33+
34+
The final `result` is `"ba"`. The character at index `k = 1` is `'a'`.
35+
36+
**Example 2:**
37+
38+
**Input:** s = "cd%#\*#", k = 3
39+
40+
**Output:** "d"
41+
42+
**Explanation:**
43+
44+
| i | s[i] | Operation | Current `result` |
45+
|---|-------|----------------------------|------------------|
46+
| 0 | `'c'` | Append `'c'` | `"c"` |
47+
| 1 | `'d'` | Append `'d'` | `"cd"` |
48+
| 2 | `'%'` | Reverse `result` | `"dc"` |
49+
| 3 | `'#'` | Duplicate `result` | `"dcdc"` |
50+
| 4 | `'*'` | Remove the last character | `"dcd"` |
51+
| 5 | `'#'` | Duplicate `result` | `"dcddcd"` |
52+
53+
The final `result` is `"dcddcd"`. The character at index `k = 3` is `'d'`.
54+
55+
**Example 3:**
56+
57+
**Input:** s = "z\*#", k = 0
58+
59+
**Output:** "."
60+
61+
**Explanation:**
62+
63+
| i | s[i] | Operation | Current `result` |
64+
|---|-------|---------------------------|------------------|
65+
| 0 | `'z'` | Append `'z'` | `"z"` |
66+
| 1 | `'*'` | Remove the last character | `""` |
67+
| 2 | `'#'` | Duplicate the string | `""` |
68+
69+
The final `result` is `""`. Since index `k = 0` is out of bounds, the output is `'.'`.
70+
71+
**Constraints:**
72+
73+
* <code>1 <= s.length <= 10<sup>5</sup></code>
74+
* `s` consists of only lowercase English letters and special characters `'*'`, `'#'`, and `'%'`.
75+
* <code>0 <= k <= 10<sup>15</sup></code>
76+
* The length of `result` after processing `s` will not exceed <code>10<sup>15</sup></code>.

0 commit comments

Comments
(0)

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