Skip to content

Commit e20d78b

Browse files
committed
Added tasks 3597-3600
1 parent d5bd433 commit e20d78b

File tree

12 files changed

+566
-0
lines changed

12 files changed

+566
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3501_3600.s3597_partition_string;
2+
3+
// #Medium #2025_06_29_Time_27_ms_(99.70%)_Space_57.45_MB_(6.81%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private static class Trie {
10+
public Trie[] trie = new Trie[26];
11+
}
12+
13+
public List<String> partitionString(String s) {
14+
var trie = new Trie();
15+
var res = new ArrayList<String>();
16+
var node = trie;
17+
for (int i = 0, j = 0; i < s.length() && j < s.length(); ) {
18+
int idx = s.charAt(j) - 'a';
19+
if (node.trie[idx] == null) {
20+
res.add(s.substring(i, j + 1));
21+
node.trie[idx] = new Trie();
22+
i = j + 1;
23+
j = i;
24+
node = trie;
25+
continue;
26+
} else {
27+
node = node.trie[idx];
28+
j++;
29+
}
30+
}
31+
return res;
32+
}
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3597\. Partition String
2+
3+
Medium
4+
5+
Given a string `s`, partition it into **unique segments** according to the following procedure:
6+
7+
* Start building a segment beginning at index 0.
8+
* Continue extending the current segment character by character until the current segment has not been seen before.
9+
* Once the segment is unique, add it to your list of segments, mark it as seen, and begin a new segment from the next index.
10+
* Repeat until you reach the end of `s`.
11+
12+
Return an array of strings `segments`, where `segments[i]` is the <code>i<sup>th</sup></code> segment created.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "abbccccd"
17+
18+
**Output:** ["a","b","bc","c","cc","d"]
19+
20+
**Explanation:**
21+
22+
Here is your table, converted from HTML to Markdown:
23+
24+
| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
25+
|-------|----------------------|-----------------------|------------------------------|-------------|----------------------------------|
26+
| 0 | "a" | [] | No | "" | ["a"] |
27+
| 1 | "b" | ["a"] | No | "" | ["a", "b"] |
28+
| 2 | "b" | ["a", "b"] | Yes | "b" | ["a", "b"] |
29+
| 3 | "bc" | ["a", "b"] | No | "" | ["a", "b", "bc"] |
30+
| 4 | "c" | ["a", "b", "bc"] | No | "" | ["a", "b", "bc", "c"] |
31+
| 5 | "c" | ["a", "b", "bc", "c"] | Yes | "c" | ["a", "b", "bc", "c"] |
32+
| 6 | "cc" | ["a", "b", "bc", "c"] | No | "" | ["a", "b", "bc", "c", "cc"] |
33+
| 7 | "d" | ["a", "b", "bc", "c", "cc"] | No | "" | ["a", "b", "bc", "c", "cc", "d"] |
34+
35+
Hence, the final output is `["a", "b", "bc", "c", "cc", "d"]`.
36+
37+
**Example 2:**
38+
39+
**Input:** s = "aaaa"
40+
41+
**Output:** ["a","aa"]
42+
43+
**Explanation:**
44+
45+
Here is your table converted to Markdown:
46+
47+
| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
48+
|-------|----------------------|---------------|------------------------------|-------------|----------------------|
49+
| 0 | "a" | [] | No | "" | ["a"] |
50+
| 1 | "a" | ["a"] | Yes | "a" | ["a"] |
51+
| 2 | "aa" | ["a"] | No | "" | ["a", "aa"] |
52+
| 3 | "a" | ["a", "aa"] | Yes | "a" | ["a", "aa"] |
53+
54+
Hence, the final output is `["a", "aa"]`.
55+
56+
**Constraints:**
57+
58+
* <code>1 <= s.length <= 10<sup>5</sup></code>
59+
* `s` contains only lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3501_3600.s3598_longest_common_prefix_between_adjacent_strings_after_removals;
2+
3+
// #Medium #2025_06_29_Time_28_ms_(74.57%)_Space_66.99_MB_(42.36%)
4+
5+
public class Solution {
6+
private int solve(String a, String b) {
7+
int len = Math.min(a.length(), b.length());
8+
int cnt = 0;
9+
while (cnt < len && a.charAt(cnt) == b.charAt(cnt)) {
10+
cnt++;
11+
}
12+
return cnt;
13+
}
14+
15+
public int[] longestCommonPrefix(String[] words) {
16+
int n = words.length;
17+
int[] ans = new int[n];
18+
if (n <= 1) {
19+
return ans;
20+
}
21+
int[] lcp = new int[n - 1];
22+
for (int i = 0; i + 1 < n; i++) {
23+
lcp[i] = solve(words[i], words[i + 1]);
24+
}
25+
int[] prefmax = new int[n - 1];
26+
int[] sufmax = new int[n - 1];
27+
prefmax[0] = lcp[0];
28+
for (int i = 1; i < n - 1; i++) {
29+
prefmax[i] = Math.max(prefmax[i - 1], lcp[i]);
30+
}
31+
sufmax[n - 2] = lcp[n - 2];
32+
for (int i = n - 3; i >= 0; i--) {
33+
sufmax[i] = Math.max(sufmax[i + 1], lcp[i]);
34+
}
35+
for (int i = 0; i < n; i++) {
36+
int best = 0;
37+
if (i >= 2) {
38+
best = Math.max(best, prefmax[i - 2]);
39+
}
40+
if (i + 1 <= n - 2) {
41+
best = Math.max(best, sufmax[i + 1]);
42+
}
43+
if (i > 0 && i < n - 1) {
44+
best = Math.max(best, solve(words[i - 1], words[i + 1]));
45+
}
46+
ans[i] = best;
47+
}
48+
return ans;
49+
}
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3598\. Longest Common Prefix Between Adjacent Strings After Removals
2+
3+
Medium
4+
5+
You are given an array of strings `words`. For each index `i` in the range `[0, words.length - 1]`, perform the following steps:
6+
7+
* Remove the element at index `i` from the `words` array.
8+
* Compute the **length** of the **longest common prefix** among all **adjacent** pairs in the modified array.
9+
10+
Return an array `answer`, where `answer[i]` is the length of the longest common prefix between the adjacent pairs after removing the element at index `i`. If **no** adjacent pairs remain or if **none** share a common prefix, then `answer[i]` should be 0.
11+
12+
**Example 1:**
13+
14+
**Input:** words = ["jump","run","run","jump","run"]
15+
16+
**Output:** [3,0,0,3,3]
17+
18+
**Explanation:**
19+
20+
* Removing index 0:
21+
* `words` becomes `["run", "run", "jump", "run"]`
22+
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)
23+
* Removing index 1:
24+
* `words` becomes `["jump", "run", "jump", "run"]`
25+
* No adjacent pairs share a common prefix (length 0)
26+
* Removing index 2:
27+
* `words` becomes `["jump", "run", "jump", "run"]`
28+
* No adjacent pairs share a common prefix (length 0)
29+
* Removing index 3:
30+
* `words` becomes `["jump", "run", "run", "run"]`
31+
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)
32+
* Removing index 4:
33+
* words becomes `["jump", "run", "run", "jump"]`
34+
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)
35+
36+
**Example 2:**
37+
38+
**Input:** words = ["dog","racer","car"]
39+
40+
**Output:** [0,0,0]
41+
42+
**Explanation:**
43+
44+
* Removing any index results in an answer of 0.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= words.length <= 10<sup>5</sup></code>
49+
* <code>1 <= words[i].length <= 10<sup>4</sup></code>
50+
* `words[i]` consists of lowercase English letters.
51+
* The sum of `words[i].length` is smaller than or equal <code>10<sup>5</sup></code>.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3501_3600.s3599_partition_array_to_minimize_xor;
2+
3+
// #Medium #2025_06_29_Time_141_ms_(89.34%)_Space_45.26_MB_(46.16%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minXor(int[] nums, int k) {
9+
int n = nums.length;
10+
// Step 1: Prefix XOR array
11+
int[] pfix = new int[n + 1];
12+
for (int i = 1; i <= n; i++) {
13+
pfix[i] = pfix[i - 1] ^ nums[i - 1];
14+
}
15+
// Step 2: DP table
16+
int[][] dp = new int[n + 1][k + 1];
17+
for (int[] row : dp) {
18+
Arrays.fill(row, Integer.MAX_VALUE);
19+
}
20+
for (int i = 0; i <= n; i++) {
21+
// Base case: 1 partition
22+
dp[i][1] = pfix[i];
23+
}
24+
// Step 3: Fill DP for partitions 2 to k
25+
for (int parts = 2; parts <= k; parts++) {
26+
for (int end = parts; end <= n; end++) {
27+
for (int split = parts - 1; split < end; split++) {
28+
int segmentXOR = pfix[end] ^ pfix[split];
29+
int maxXOR = Math.max(dp[split][parts - 1], segmentXOR);
30+
dp[end][parts] = Math.min(dp[end][parts], maxXOR);
31+
}
32+
}
33+
}
34+
return dp[n][k];
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
3599\. Partition Array to Minimize XOR
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Your task is to partition `nums` into `k` non-empty ****non-empty subarrays****. For each subarray, compute the bitwise **XOR** of all its elements.
8+
9+
Return the **minimum** possible value of the **maximum XOR** among these `k` subarrays.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3], k = 2
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The optimal partition is `[1]` and `[2, 3]`.
20+
21+
* XOR of the first subarray is `1`.
22+
* XOR of the second subarray is `2 XOR 3 = 1`.
23+
24+
The maximum XOR among the subarrays is 1, which is the minimum possible.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [2,3,3,2], k = 3
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
The optimal partition is `[2]`, `[3, 3]`, and `[2]`.
35+
36+
* XOR of the first subarray is `2`.
37+
* XOR of the second subarray is `3 XOR 3 = 0`.
38+
* XOR of the third subarray is `2`.
39+
40+
The maximum XOR among the subarrays is 2, which is the minimum possible.
41+
42+
**Example 3:**
43+
44+
**Input:** nums = [1,1,2,3,1], k = 2
45+
46+
**Output:** 0
47+
48+
**Explanation:**
49+
50+
The optimal partition is `[1, 1]` and `[2, 3, 1]`.
51+
52+
* XOR of the first subarray is `1 XOR 1 = 0`.
53+
* XOR of the second subarray is `2 XOR 3 XOR 1 = 0`.
54+
55+
The maximum XOR among the subarrays is 0, which is the minimum possible.
56+
57+
**Constraints:**
58+
59+
* `1 <= nums.length <= 250`
60+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
61+
* `1 <= k <= n`

0 commit comments

Comments
 (0)