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 cdb6594

Browse files
authored
Added tasks 3597-3600
1 parent d5bd433 commit cdb6594

File tree

12 files changed

+566
-0
lines changed

12 files changed

+566
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3501_3600.s3597_partition_string;
2+
3+
// #Medium #String #Hash_Table #Simulation #Trie
4+
// #2025_06_30_Time_25_ms_(100.00%)_Space_55.91_MB_(100.00%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private static class Trie {
11+
Trie[] tries = new Trie[26];
12+
}
13+
14+
public List<String> partitionString(String s) {
15+
Trie trie = new Trie();
16+
List<String> res = new ArrayList<>();
17+
Trie node = trie;
18+
int i = 0;
19+
int j = 0;
20+
while (i < s.length() && j < s.length()) {
21+
int idx = s.charAt(j) - 'a';
22+
if (node.tries[idx] == null) {
23+
res.add(s.substring(i, j + 1));
24+
node.tries[idx] = new Trie();
25+
i = j + 1;
26+
j = i;
27+
node = trie;
28+
} else {
29+
node = node.tries[idx];
30+
j++;
31+
}
32+
}
33+
return res;
34+
}
35+
}
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 #Array #String #2025_06_30_Time_28_ms_(74.57%)_Space_67.08_MB_(39.11%)
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3501_3600.s3599_partition_array_to_minimize_xor;
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Prefix_Sum
4+
// #2025_06_30_Time_144_ms_(100.00%)_Space_44.80_MB_(100.00%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int minXor(int[] nums, int k) {
10+
int n = nums.length;
11+
// Step 1: Prefix XOR array
12+
int[] pfix = new int[n + 1];
13+
for (int i = 1; i <= n; i++) {
14+
pfix[i] = pfix[i - 1] ^ nums[i - 1];
15+
}
16+
// Step 2: DP table
17+
int[][] dp = new int[n + 1][k + 1];
18+
for (int[] row : dp) {
19+
Arrays.fill(row, Integer.MAX_VALUE);
20+
}
21+
for (int i = 0; i <= n; i++) {
22+
// Base case: 1 partition
23+
dp[i][1] = pfix[i];
24+
}
25+
// Step 3: Fill DP for partitions 2 to k
26+
for (int parts = 2; parts <= k; parts++) {
27+
for (int end = parts; end <= n; end++) {
28+
for (int split = parts - 1; split < end; split++) {
29+
int segmentXOR = pfix[end] ^ pfix[split];
30+
int maxXOR = Math.max(dp[split][parts - 1], segmentXOR);
31+
dp[end][parts] = Math.min(dp[end][parts], maxXOR);
32+
}
33+
}
34+
}
35+
return dp[n][k];
36+
}
37+
}
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)

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