Skip to content

Added tasks 3597-3600 #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/kotlin/g3501_3600/s3597_partition_string/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3501_3600.s3597_partition_string

// #Medium #String #Hash_Table #Simulation #Trie
// #2025_06_30_Time_43_ms_(100.00%)_Space_61.32_MB_(100.00%)

class Solution {
private class Trie {
var tries: Array<Trie?> = arrayOfNulls<Trie>(26)
}

fun partitionString(s: String): List<String> {
val trie = Trie()
val res: MutableList<String> = ArrayList()
var node: Trie = trie
var i = 0
var j = 0
while (i < s.length && j < s.length) {
val idx = s[j].code - 'a'.code
if (node.tries[idx] == null) {
res.add(s.substring(i, j + 1))
node.tries[idx] = Trie()
i = j + 1
j = i
node = trie
} else {
node = node.tries[idx]!!
j++
}
}
return res
}
}
59 changes: 59 additions & 0 deletions src/main/kotlin/g3501_3600/s3597_partition_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3597\. Partition String

Medium

Given a string `s`, partition it into **unique segments** according to the following procedure:

* Start building a segment beginning at index 0.
* Continue extending the current segment character by character until the current segment has not been seen before.
* 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.
* Repeat until you reach the end of `s`.

Return an array of strings `segments`, where `segments[i]` is the <code>i<sup>th</sup></code> segment created.

**Example 1:**

**Input:** s = "abbccccd"

**Output:** ["a","b","bc","c","cc","d"]

**Explanation:**

Here is your table, converted from HTML to Markdown:

| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
|-------|----------------------|-----------------------|------------------------------|-------------|----------------------------------|
| 0 | "a" | [] | No | "" | ["a"] |
| 1 | "b" | ["a"] | No | "" | ["a", "b"] |
| 2 | "b" | ["a", "b"] | Yes | "b" | ["a", "b"] |
| 3 | "bc" | ["a", "b"] | No | "" | ["a", "b", "bc"] |
| 4 | "c" | ["a", "b", "bc"] | No | "" | ["a", "b", "bc", "c"] |
| 5 | "c" | ["a", "b", "bc", "c"] | Yes | "c" | ["a", "b", "bc", "c"] |
| 6 | "cc" | ["a", "b", "bc", "c"] | No | "" | ["a", "b", "bc", "c", "cc"] |
| 7 | "d" | ["a", "b", "bc", "c", "cc"] | No | "" | ["a", "b", "bc", "c", "cc", "d"] |

Hence, the final output is `["a", "b", "bc", "c", "cc", "d"]`.

**Example 2:**

**Input:** s = "aaaa"

**Output:** ["a","aa"]

**Explanation:**

Here is your table converted to Markdown:

| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
|-------|----------------------|---------------|------------------------------|-------------|----------------------|
| 0 | "a" | [] | No | "" | ["a"] |
| 1 | "a" | ["a"] | Yes | "a" | ["a"] |
| 2 | "aa" | ["a"] | No | "" | ["a", "aa"] |
| 3 | "a" | ["a", "aa"] | Yes | "a" | ["a", "aa"] |

Hence, the final output is `["a", "aa"]`.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` contains only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3501_3600.s3598_longest_common_prefix_between_adjacent_strings_after_removals

// #Medium #Array #String #2025_06_30_Time_28_ms_(71.43%)_Space_81.21_MB_(71.43%)

import kotlin.math.max
import kotlin.math.min

class Solution {
private fun solve(a: String, b: String): Int {
val len = min(a.length, b.length)
var cnt = 0
while (cnt < len && a[cnt] == b[cnt]) {
cnt++
}
return cnt
}

fun longestCommonPrefix(words: Array<String>): IntArray {
val n = words.size
val ans = IntArray(n)
if (n <= 1) {
return ans
}
val lcp = IntArray(n - 1)
run {
var i = 0
while (i + 1 < n) {
lcp[i] = solve(words[i], words[i + 1])
i++
}
}
val prefmax = IntArray(n - 1)
val sufmax = IntArray(n - 1)
prefmax[0] = lcp[0]
for (i in 1..<n - 1) {
prefmax[i] = max(prefmax[i - 1], lcp[i])
}
sufmax[n - 2] = lcp[n - 2]
for (i in n - 3 downTo 0) {
sufmax[i] = max(sufmax[i + 1], lcp[i])
}
for (i in 0..<n) {
var best = 0
if (i >= 2) {
best = max(best, prefmax[i - 2])
}
if (i + 1 <= n - 2) {
best = max(best, sufmax[i + 1])
}
if (i > 0 && i < n - 1) {
best = max(best, solve(words[i - 1], words[i + 1]))
}
ans[i] = best
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3598\. Longest Common Prefix Between Adjacent Strings After Removals

Medium

You are given an array of strings `words`. For each index `i` in the range `[0, words.length - 1]`, perform the following steps:

* Remove the element at index `i` from the `words` array.
* Compute the **length** of the **longest common prefix** among all **adjacent** pairs in the modified array.

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.

**Example 1:**

**Input:** words = ["jump","run","run","jump","run"]

**Output:** [3,0,0,3,3]

**Explanation:**

* Removing index 0:
* `words` becomes `["run", "run", "jump", "run"]`
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)
* Removing index 1:
* `words` becomes `["jump", "run", "jump", "run"]`
* No adjacent pairs share a common prefix (length 0)
* Removing index 2:
* `words` becomes `["jump", "run", "jump", "run"]`
* No adjacent pairs share a common prefix (length 0)
* Removing index 3:
* `words` becomes `["jump", "run", "run", "run"]`
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)
* Removing index 4:
* words becomes `["jump", "run", "run", "jump"]`
* Longest adjacent pair is `["run", "run"]` having a common prefix `"run"` (length 3)

**Example 2:**

**Input:** words = ["dog","racer","car"]

**Output:** [0,0,0]

**Explanation:**

* Removing any index results in an answer of 0.

**Constraints:**

* <code>1 <= words.length <= 10<sup>5</sup></code>
* <code>1 <= words[i].length <= 10<sup>4</sup></code>
* `words[i]` consists of lowercase English letters.
* The sum of `words[i].length` is smaller than or equal <code>10<sup>5</sup></code>.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g3501_3600.s3599_partition_array_to_minimize_xor

// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Prefix_Sum
// #2025_06_30_Time_136_ms_(100.00%)_Space_54.66_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun minXor(nums: IntArray, k: Int): Int {
val n = nums.size
// Step 1: Prefix XOR array
val pfix = IntArray(n + 1)
for (i in 1..n) {
pfix[i] = pfix[i - 1] xor nums[i - 1]
}
// Step 2: DP table
val dp: Array<IntArray> = Array(n + 1) { IntArray(k + 1) }
for (row in dp) {
row.fill(Int.Companion.MAX_VALUE)
}
for (i in 0..n) {
// Base case: 1 partition
dp[i][1] = pfix[i]
}
// Step 3: Fill DP for partitions 2 to k
for (parts in 2..k) {
for (end in parts..n) {
for (split in parts - 1..<end) {
val segmentXOR = pfix[end] xor pfix[split]
val maxXOR = max(dp[split][parts - 1], segmentXOR)
dp[end][parts] = min(dp[end][parts], maxXOR)
}
}
}
return dp[n][k]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
3599\. Partition Array to Minimize XOR

Medium

You are given an integer array `nums` and an integer `k`.

Your task is to partition `nums` into `k` non-empty ****non-empty subarrays****. For each subarray, compute the bitwise **XOR** of all its elements.

Return the **minimum** possible value of the **maximum XOR** among these `k` subarrays.

**Example 1:**

**Input:** nums = [1,2,3], k = 2

**Output:** 1

**Explanation:**

The optimal partition is `[1]` and `[2, 3]`.

* XOR of the first subarray is `1`.
* XOR of the second subarray is `2 XOR 3 = 1`.

The maximum XOR among the subarrays is 1, which is the minimum possible.

**Example 2:**

**Input:** nums = [2,3,3,2], k = 3

**Output:** 2

**Explanation:**

The optimal partition is `[2]`, `[3, 3]`, and `[2]`.

* XOR of the first subarray is `2`.
* XOR of the second subarray is `3 XOR 3 = 0`.
* XOR of the third subarray is `2`.

The maximum XOR among the subarrays is 2, which is the minimum possible.

**Example 3:**

**Input:** nums = [1,1,2,3,1], k = 2

**Output:** 0

**Explanation:**

The optimal partition is `[1, 1]` and `[2, 3, 1]`.

* XOR of the first subarray is `1 XOR 1 = 0`.
* XOR of the second subarray is `2 XOR 3 XOR 1 = 0`.

The maximum XOR among the subarrays is 0, which is the minimum possible.

**Constraints:**

* `1 <= nums.length <= 250`
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* `1 <= k <= n`
Loading