Skip to content

Commit 08be811

Browse files
authored
Added tasks 3556-3663
1 parent c56c524 commit 08be811

File tree

24 files changed

+1139
-0
lines changed

24 files changed

+1139
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g3501_3600.s3556_sum_of_largest_prime_substrings
2+
3+
// #Medium #String #Hash_Table #Math #Sorting #Number_Theory
4+
// #2025_05_25_Time_25_ms_(100.00%)_Space_43.67_MB_(100.00%)
5+
6+
class Solution {
7+
fun sumOfLargestPrimes(s: String): Long {
8+
val set: MutableSet<Long> = HashSet()
9+
val n = s.length
10+
var first: Long = -1
11+
var second: Long = -1
12+
var third: Long = -1
13+
for (i in 0..<n) {
14+
var num: Long = 0
15+
for (j in i..<n) {
16+
num = num * 10 + (s[j].code - '0'.code)
17+
if (i != j && s[i] == '0') {
18+
break
19+
}
20+
if (isPrime(num) && !set.contains(num)) {
21+
set.add(num)
22+
if (num > first) {
23+
third = second
24+
second = first
25+
first = num
26+
} else if (num > second) {
27+
third = second
28+
second = num
29+
} else if (num > third) {
30+
third = num
31+
}
32+
}
33+
}
34+
}
35+
var sum: Long = 0
36+
if (first != -1L) {
37+
sum += first
38+
}
39+
if (second != -1L) {
40+
sum += second
41+
}
42+
if (third != -1L) {
43+
sum += third
44+
}
45+
return sum
46+
}
47+
48+
fun isPrime(num: Long): Boolean {
49+
if (num <= 1) {
50+
return false
51+
}
52+
if (num == 2L || num == 3L) {
53+
return true
54+
}
55+
if (num % 2 == 0L || num % 3 == 0L) {
56+
return false
57+
}
58+
var i: Long = 5
59+
while (i * i <= num) {
60+
if (num % i == 0L || num % (i + 2) == 0L) {
61+
return false
62+
}
63+
i += 6
64+
}
65+
return true
66+
}
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3556\. Sum of Largest Prime Substrings
2+
3+
Medium
4+
5+
Given a string `s`, find the sum of the **3 largest unique prime numbers** that can be formed using any of its ****substring****.
6+
7+
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.
8+
9+
**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.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "12234"
14+
15+
**Output:** 1469
16+
17+
**Explanation:**
18+
19+
* The unique prime numbers formed from the substrings of `"12234"` are 2, 3, 23, 223, and 1223.
20+
* The 3 largest primes are 1223, 223, and 23. Their sum is 1469.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "111"
25+
26+
**Output:** 11
27+
28+
**Explanation:**
29+
30+
* The unique prime number formed from the substrings of `"111"` is 11.
31+
* Since there is only one prime number, the sum is 11.
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 10`
36+
* `s` consists of only digits.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3501_3600.s3557_find_maximum_number_of_non_intersecting_substrings
2+
3+
// #Medium #String #Hash_Table #Dynamic_Programming #Greedy
4+
// #2025_05_27_Time_28_ms_(70.59%)_Space_49.63_MB_(70.59%)
5+
6+
class Solution {
7+
fun maxSubstrings(s: String): Int {
8+
val prev = IntArray(26)
9+
var r = 0
10+
prev.fill(-1)
11+
for (i in 0..<s.length) {
12+
val j = s[i].code - 'a'.code
13+
if (prev[j] != -1 && i - prev[j] + 1 >= 4) {
14+
++r
15+
prev.fill(-1)
16+
} else if (prev[j] == -1) {
17+
prev[j] = i
18+
}
19+
}
20+
return r
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3557\. Find Maximum Number of Non Intersecting Substrings
2+
3+
Medium
4+
5+
You are given a string `word`.
6+
7+
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.
8+
9+
**Example 1:**
10+
11+
**Input:** word = "abcdeafdef"
12+
13+
**Output:** 2
14+
15+
**Explanation:**
16+
17+
The two substrings are `"abcdea"` and `"fdef"`.
18+
19+
**Example 2:**
20+
21+
**Input:** word = "bcdaaaab"
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
The only substring is `"aaaa"`. Note that we cannot **also** choose `"bcdaaaab"` since it intersects with the other substring.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
32+
* `word` consists only of lowercase English letters.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3501_3600.s3558_number_of_ways_to_assign_edge_weights_i
2+
3+
// #Medium #Math #Depth_First_Search #Tree #2025_05_27_Time_21_ms_(100.00%)_Space_135.14_MB_(45.45%)
4+
5+
class Solution {
6+
fun assignEdgeWeights(edges: Array<IntArray>): Int {
7+
if (pow2[0] == 0L) {
8+
pow2[0] = 1
9+
for (i in 1..<pow2.size) {
10+
pow2[i] = (pow2[i - 1] shl 1) % mod
11+
}
12+
}
13+
val n = edges.size + 1
14+
val adj = IntArray(n + 1)
15+
val degrees = IntArray(n + 1)
16+
for (edge in edges) {
17+
val u = edge[0]
18+
val v = edge[1]
19+
adj[u] += v
20+
adj[v] += u
21+
degrees[u]++
22+
degrees[v]++
23+
}
24+
val que = IntArray(n)
25+
var write = 0
26+
var read = 0
27+
for (i in 2..n) {
28+
if (degrees[i] == 1) {
29+
que[write++] = i
30+
}
31+
}
32+
var distance = 0
33+
while (read < write) {
34+
distance++
35+
var size = write - read
36+
while (size-- > 0) {
37+
val v = que[read++]
38+
val u = adj[v]
39+
adj[u] -= v
40+
if (--degrees[u] == 1 && u != 1) {
41+
que[write++] = u
42+
}
43+
}
44+
}
45+
return pow2[distance - 1].toInt()
46+
}
47+
48+
companion object {
49+
private const val mod = 1e9.toInt() + 7
50+
private val pow2 = LongArray(100001)
51+
}
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3558\. Number of Ways to Assign Edge Weights I
2+
3+
Medium
4+
5+
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>.
6+
7+
Initially, all edges have a weight of 0. You must assign each edge a weight of either **1** or **2**.
8+
9+
The **cost** of a path between any two nodes `u` and `v` is the total weight of all edges in the path connecting them.
10+
11+
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**.
12+
13+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
14+
15+
**Note:** Ignore all edges **not** in the path from node 1 to `x`.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-060006.png)
20+
21+
**Input:** edges = [[1,2]]
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
* The path from Node 1 to Node 2 consists of one edge (`1 → 2`).
28+
* Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-055820.png)
33+
34+
**Input:** edges = [[1,2],[1,3],[3,4],[3,5]]
35+
36+
**Output:** 2
37+
38+
**Explanation:**
39+
40+
* The maximum depth is 2, with nodes 4 and 5 at the same depth. Either node can be selected for processing.
41+
* For example, the path from Node 1 to Node 4 consists of two edges (`1 → 3` and `3 → 4`).
42+
* Assigning weights (1,2) or (2,1) results in an odd cost. Thus, the number of valid assignments is 2.
43+
44+
**Constraints:**
45+
46+
* <code>2 <= n <= 10<sup>5</sup></code>
47+
* `edges.length == n - 1`
48+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>]</code>
49+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
50+
* `edges` represents a valid tree.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package g3501_3600.s3559_number_of_ways_to_assign_edge_weights_ii
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Depth_First_Search #Tree
4+
// #2025_05_25_Time_197_ms_(100.00%)_Space_158.27_MB_(100.00%)
5+
6+
import kotlin.math.ceil
7+
import kotlin.math.ln
8+
9+
class Solution {
10+
private lateinit var adj: MutableList<MutableList<Int>>
11+
private lateinit var level: IntArray
12+
private lateinit var jumps: Array<IntArray?>
13+
14+
private fun mark(node: Int, par: Int) {
15+
for (neigh in adj[node]) {
16+
if (neigh == par) {
17+
continue
18+
}
19+
level[neigh] = level[node] + 1
20+
jumps[neigh]!![0] = node
21+
mark(neigh, node)
22+
}
23+
}
24+
25+
fun lift(u: Int, diff: Int): Int {
26+
var u = u
27+
var diff = diff
28+
while (diff > 0) {
29+
val rightmost = diff xor (diff and (diff - 1))
30+
val jump = (ln(rightmost.toDouble()) / ln(2.0)).toInt()
31+
u = jumps[u]!![jump]
32+
diff -= rightmost
33+
}
34+
return u
35+
}
36+
37+
private fun findLca(u: Int, v: Int): Int {
38+
var u = u
39+
var v = v
40+
if (level[u] > level[v]) {
41+
val temp = u
42+
u = v
43+
v = temp
44+
}
45+
v = lift(v, level[v] - level[u])
46+
if (u == v) {
47+
return u
48+
}
49+
for (i in jumps[0]!!.indices.reversed()) {
50+
if (jumps[u]!![i] != jumps[v]!![i]) {
51+
u = jumps[u]!![i]
52+
v = jumps[v]!![i]
53+
}
54+
}
55+
return jumps[u]!![0]
56+
}
57+
58+
private fun findDist(a: Int, b: Int): Int {
59+
return level[a] + level[b] - 2 * level[findLca(a, b)]
60+
}
61+
62+
fun assignEdgeWeights(edges: Array<IntArray>, queries: Array<IntArray>): IntArray {
63+
val n = edges.size + 1
64+
adj = ArrayList<MutableList<Int>>()
65+
level = IntArray(n)
66+
for (i in 0..<n) {
67+
adj.add(ArrayList<Int>())
68+
}
69+
for (i in edges) {
70+
adj[i[0] - 1].add(i[1] - 1)
71+
adj[i[1] - 1].add(i[0] - 1)
72+
}
73+
val m = (ceil(ln(n - 1.0) / ln(2.0))).toInt() + 1
74+
jumps = Array<IntArray?>(n) { IntArray(m) }
75+
mark(0, -1)
76+
for (j in 1..<m) {
77+
for (i in 0..<n) {
78+
val p = jumps[i]!![j - 1]
79+
jumps[i]!![j] = jumps[p]!![j - 1]
80+
}
81+
}
82+
val pow = IntArray(n + 1)
83+
pow[0] = 1
84+
for (i in 1..n) {
85+
pow[i] = (pow[i - 1] * 2) % MOD
86+
}
87+
val q = queries.size
88+
val ans = IntArray(q)
89+
for (i in 0..<q) {
90+
val d = findDist(queries[i][0] - 1, queries[i][1] - 1)
91+
ans[i] = if (d > 0) pow[d - 1] else 0
92+
}
93+
return ans
94+
}
95+
96+
companion object {
97+
private const val MOD = 1000000007
98+
}
99+
}

0 commit comments

Comments
 (0)