Skip to content

Added tasks 3556-3663 #819

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 6 commits into from
May 27, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package g3501_3600.s3556_sum_of_largest_prime_substrings

// #Medium #String #Hash_Table #Math #Sorting #Number_Theory
// #2025_05_25_Time_25_ms_(100.00%)_Space_43.67_MB_(100.00%)

class Solution {
fun sumOfLargestPrimes(s: String): Long {
val set: MutableSet<Long> = HashSet()
val n = s.length
var first: Long = -1
var second: Long = -1
var third: Long = -1
for (i in 0..<n) {
var num: Long = 0
for (j in i..<n) {
num = num * 10 + (s[j].code - '0'.code)
if (i != j && s[i] == '0') {
break
}
if (isPrime(num) && !set.contains(num)) {
set.add(num)
if (num > first) {
third = second
second = first
first = num
} else if (num > second) {
third = second
second = num
} else if (num > third) {
third = num
}
}
}
}
var sum: Long = 0
if (first != -1L) {
sum += first
}
if (second != -1L) {
sum += second
}
if (third != -1L) {
sum += third
}
return sum
}

fun isPrime(num: Long): Boolean {
if (num <= 1) {
return false
}
if (num == 2L || num == 3L) {
return true
}
if (num % 2 == 0L || num % 3 == 0L) {
return false
}
var i: Long = 5
while (i * i <= num) {
if (num % i == 0L || num % (i + 2) == 0L) {
return false
}
i += 6
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
3556\. Sum of Largest Prime Substrings

Medium

Given a string `s`, find the sum of the **3 largest unique prime numbers** that can be formed using any of its ****substring****.

Return the **sum** of the three largest unique prime numbers that can be formed. If fewer than three exist, return the sum of **all** available primes. If no prime numbers can be formed, return 0.

**Note:** Each prime number should be counted only **once**, even if it appears in **multiple** substrings. Additionally, when converting a substring to an integer, any leading zeros are ignored.

**Example 1:**

**Input:** s = "12234"

**Output:** 1469

**Explanation:**

* The unique prime numbers formed from the substrings of `"12234"` are 2, 3, 23, 223, and 1223.
* The 3 largest primes are 1223, 223, and 23. Their sum is 1469.

**Example 2:**

**Input:** s = "111"

**Output:** 11

**Explanation:**

* The unique prime number formed from the substrings of `"111"` is 11.
* Since there is only one prime number, the sum is 11.

**Constraints:**

* `1 <= s.length <= 10`
* `s` consists of only digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3501_3600.s3557_find_maximum_number_of_non_intersecting_substrings

// #Medium #String #Hash_Table #Dynamic_Programming #Greedy
// #2025_05_27_Time_28_ms_(70.59%)_Space_49.63_MB_(70.59%)

class Solution {
fun maxSubstrings(s: String): Int {
val prev = IntArray(26)
var r = 0
prev.fill(-1)
for (i in 0..<s.length) {
val j = s[i].code - 'a'.code
if (prev[j] != -1 && i - prev[j] + 1 >= 4) {
++r
prev.fill(-1)
} else if (prev[j] == -1) {
prev[j] = i
}
}
return r
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3557\. Find Maximum Number of Non Intersecting Substrings

Medium

You are given a string `word`.

Return the **maximum** number of non-intersecting ****substring**** of word that are at **least** four characters long and start and end with the same letter.

**Example 1:**

**Input:** word = "abcdeafdef"

**Output:** 2

**Explanation:**

The two substrings are `"abcdea"` and `"fdef"`.

**Example 2:**

**Input:** word = "bcdaaaab"

**Output:** 1

**Explanation:**

The only substring is `"aaaa"`. Note that we cannot **also** choose `"bcdaaaab"` since it intersects with the other substring.

**Constraints:**

* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
* `word` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package g3501_3600.s3558_number_of_ways_to_assign_edge_weights_i

// #Medium #Math #Depth_First_Search #Tree #2025_05_27_Time_21_ms_(100.00%)_Space_135.14_MB_(45.45%)

class Solution {
fun assignEdgeWeights(edges: Array<IntArray>): Int {
if (pow2[0] == 0L) {
pow2[0] = 1
for (i in 1..<pow2.size) {
pow2[i] = (pow2[i - 1] shl 1) % mod
}
}
val n = edges.size + 1
val adj = IntArray(n + 1)
val degrees = IntArray(n + 1)
for (edge in edges) {
val u = edge[0]
val v = edge[1]
adj[u] += v
adj[v] += u
degrees[u]++
degrees[v]++
}
val que = IntArray(n)
var write = 0
var read = 0
for (i in 2..n) {
if (degrees[i] == 1) {
que[write++] = i
}
}
var distance = 0
while (read < write) {
distance++
var size = write - read
while (size-- > 0) {
val v = que[read++]
val u = adj[v]
adj[u] -= v
if (--degrees[u] == 1 && u != 1) {
que[write++] = u
}
}
}
return pow2[distance - 1].toInt()
}

companion object {
private const val mod = 1e9.toInt() + 7
private val pow2 = LongArray(100001)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3558\. Number of Ways to Assign Edge Weights I

Medium

There is an undirected tree with `n` nodes labeled from 1 to `n`, rooted at node 1. The tree is represented by a 2D integer array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.

Initially, all edges have a weight of 0. You must assign each edge a weight of either **1** or **2**.

The **cost** of a path between any two nodes `u` and `v` is the total weight of all edges in the path connecting them.

Select any one node `x` at the **maximum** depth. Return the number of ways to assign edge weights in the path from node 1 to `x` such that its total cost is **odd**.

Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Note:** Ignore all edges **not** in the path from node 1 to `x`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-060006.png)

**Input:** edges = [[1,2]]

**Output:** 1

**Explanation:**

* The path from Node 1 to Node 2 consists of one edge (`1 → 2`).
* Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.

**Example 2:**

![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-055820.png)

**Input:** edges = [[1,2],[1,3],[3,4],[3,5]]

**Output:** 2

**Explanation:**

* The maximum depth is 2, with nodes 4 and 5 at the same depth. Either node can be selected for processing.
* For example, the path from Node 1 to Node 4 consists of two edges (`1 → 3` and `3 → 4`).
* Assigning weights (1,2) or (2,1) results in an odd cost. Thus, the number of valid assignments is 2.

**Constraints:**

* <code>2 <= n <= 10<sup>5</sup></code>
* `edges.length == n - 1`
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>]</code>
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
* `edges` represents a valid tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package g3501_3600.s3559_number_of_ways_to_assign_edge_weights_ii

// #Hard #Array #Dynamic_Programming #Math #Depth_First_Search #Tree
// #2025_05_25_Time_197_ms_(100.00%)_Space_158.27_MB_(100.00%)

import kotlin.math.ceil
import kotlin.math.ln

class Solution {
private lateinit var adj: MutableList<MutableList<Int>>
private lateinit var level: IntArray
private lateinit var jumps: Array<IntArray?>

private fun mark(node: Int, par: Int) {
for (neigh in adj[node]) {
if (neigh == par) {
continue
}
level[neigh] = level[node] + 1
jumps[neigh]!![0] = node
mark(neigh, node)
}
}

fun lift(u: Int, diff: Int): Int {
var u = u
var diff = diff
while (diff > 0) {
val rightmost = diff xor (diff and (diff - 1))
val jump = (ln(rightmost.toDouble()) / ln(2.0)).toInt()
u = jumps[u]!![jump]
diff -= rightmost
}
return u
}

private fun findLca(u: Int, v: Int): Int {
var u = u
var v = v
if (level[u] > level[v]) {
val temp = u
u = v
v = temp
}
v = lift(v, level[v] - level[u])
if (u == v) {
return u
}
for (i in jumps[0]!!.indices.reversed()) {
if (jumps[u]!![i] != jumps[v]!![i]) {
u = jumps[u]!![i]
v = jumps[v]!![i]
}
}
return jumps[u]!![0]
}

private fun findDist(a: Int, b: Int): Int {
return level[a] + level[b] - 2 * level[findLca(a, b)]
}

fun assignEdgeWeights(edges: Array<IntArray>, queries: Array<IntArray>): IntArray {
val n = edges.size + 1
adj = ArrayList<MutableList<Int>>()
level = IntArray(n)
for (i in 0..<n) {
adj.add(ArrayList<Int>())
}
for (i in edges) {
adj[i[0] - 1].add(i[1] - 1)
adj[i[1] - 1].add(i[0] - 1)
}
val m = (ceil(ln(n - 1.0) / ln(2.0))).toInt() + 1
jumps = Array<IntArray?>(n) { IntArray(m) }
mark(0, -1)
for (j in 1..<m) {
for (i in 0..<n) {
val p = jumps[i]!![j - 1]
jumps[i]!![j] = jumps[p]!![j - 1]
}
}
val pow = IntArray(n + 1)
pow[0] = 1
for (i in 1..n) {
pow[i] = (pow[i - 1] * 2) % MOD
}
val q = queries.size
val ans = IntArray(q)
for (i in 0..<q) {
val d = findDist(queries[i][0] - 1, queries[i][1] - 1)
ans[i] = if (d > 0) pow[d - 1] else 0
}
return ans
}

companion object {
private const val MOD = 1000000007
}
}
Loading