Skip to content

Commit 5b0f944

Browse files
committed
Added tasks 3556-3563
1 parent e838c31 commit 5b0f944

File tree

9 files changed

+876
-0
lines changed
  • src/main/java/g3501_3600
    • s3556_sum_of_largest_prime_substrings
    • s3557_find_maximum_number_of_non_intersecting_substrings
    • s3558_number_of_ways_to_assign_edge_weights_i
    • s3559_number_of_ways_to_assign_edge_weights_ii
    • s3560_find_minimum_log_transportation_cost
    • s3561_resulting_string_after_adjacent_removals
    • s3562_maximum_profit_from_trading_stocks_with_discounts
    • s3563_lexicographically_smallest_string_after_adjacent_removals

9 files changed

+876
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3563 |[Lexicographically Smallest String After Adjacent Removals](src/main/java/g3501_3600/s3563_lexicographically_smallest_string_after_adjacent_removals)| Hard | String, Dynamic_Programming | 121 | 99.09
2092+
| 3562 |[Maximum Profit from Trading Stocks with Discounts](src/main/java/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts)| Hard | Array, Dynamic_Programming, Tree, Depth_First_Search | 27 | 100.00
2093+
| 3561 |[Resulting String After Adjacent Removals](src/main/java/g3501_3600/s3561_resulting_string_after_adjacent_removals)| Medium | String, Stack, Simulation | 36 | 100.00
2094+
| 3560 |[Find Minimum Log Transportation Cost](src/main/java/g3501_3600/s3560_find_minimum_log_transportation_cost)| Easy | Math | 0 | 100.00
2095+
| 3559 |[Number of Ways to Assign Edge Weights II](src/main/java/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii)| Hard | Array, Dynamic_Programming, Math, Tree, Depth_First_Search | 138 | 64.66
2096+
| 3558 |[Number of Ways to Assign Edge Weights I](src/main/java/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i)| Medium | Math, Tree, Depth_First_Search | 12 | 100.00
2097+
| 3557 |[Find Maximum Number of Non Intersecting Substrings](src/main/java/g3501_3600/s3557_find_maximum_number_of_non_intersecting_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Greedy | 15 | 84.54
2098+
| 3556 |[Sum of Largest Prime Substrings](src/main/java/g3501_3600/s3556_sum_of_largest_prime_substrings)| Medium | String, Hash_Table, Math, Sorting, Number_Theory | 7 | 99.93
20912099
| 3554 |[Find Category Recommendation Pairs](src/main/java/g3501_3600/s3554_find_category_recommendation_pairs)| Hard | Database | 623 | 82.76
20922100
| 3553 |[Minimum Weighted Subgraph With the Required Paths II](src/main/java/g3501_3600/s3553_minimum_weighted_subgraph_with_the_required_paths_ii)| Hard | Array, Depth_First_Search, Tree | 65 | 100.00
20932101
| 3552 |[Grid Teleportation Traversal](src/main/java/g3501_3600/s3552_grid_teleportation_traversal)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix | 146 | 98.62
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3556\. Sum of Largest Prime Substrings
5+
6+
Medium
7+
8+
Given a string `s`, find the sum of the **3 largest unique prime numbers** that can be formed using any of its ****substring****.
9+
10+
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.
11+
12+
**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.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "12234"
17+
18+
**Output:** 1469
19+
20+
**Explanation:**
21+
22+
* The unique prime numbers formed from the substrings of `"12234"` are 2, 3, 23, 223, and 1223.
23+
* The 3 largest primes are 1223, 223, and 23. Their sum is 1469.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "111"
28+
29+
**Output:** 11
30+
31+
**Explanation:**
32+
33+
* The unique prime number formed from the substrings of `"111"` is 11.
34+
* Since there is only one prime number, the sum is 11.
35+
36+
**Constraints:**
37+
38+
* `1 <= s.length <= 10`
39+
* `s` consists of only digits.
40+
41+
## Solution
42+
43+
```java
44+
import java.util.HashSet;
45+
import java.util.Set;
46+
47+
public class Solution {
48+
public long sumOfLargestPrimes(String s) {
49+
Set<Long> set = new HashSet<>();
50+
int n = s.length();
51+
long first = -1;
52+
long second = -1;
53+
long third = -1;
54+
for (int i = 0; i < n; i++) {
55+
long num = 0;
56+
for (int j = i; j < n; j++) {
57+
num = num * 10 + (s.charAt(j) - '0');
58+
if (i != j && s.charAt(i) == '0') {
59+
break;
60+
}
61+
if (isPrime(num) && !set.contains(num)) {
62+
set.add(num);
63+
if (num > first) {
64+
third = second;
65+
second = first;
66+
first = num;
67+
} else if (num > second) {
68+
third = second;
69+
second = num;
70+
} else if (num > third) {
71+
third = num;
72+
}
73+
}
74+
}
75+
}
76+
long sum = 0;
77+
if (first != -1) {
78+
sum += first;
79+
}
80+
if (second != -1) {
81+
sum += second;
82+
}
83+
if (third != -1) {
84+
sum += third;
85+
}
86+
return sum;
87+
}
88+
89+
public boolean isPrime(long num) {
90+
if (num <= 1) {
91+
return false;
92+
}
93+
if (num == 2 || num == 3) {
94+
return true;
95+
}
96+
if (num % 2 == 0 || num % 3 == 0) {
97+
return false;
98+
}
99+
for (long i = 5; i * i <= num; i += 6) {
100+
if (num % i == 0 || num % (i + 2) == 0) {
101+
return false;
102+
}
103+
}
104+
return true;
105+
}
106+
}
107+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3557\. Find Maximum Number of Non Intersecting Substrings
5+
6+
Medium
7+
8+
You are given a string `word`.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** word = "abcdeafdef"
15+
16+
**Output:** 2
17+
18+
**Explanation:**
19+
20+
The two substrings are `"abcdea"` and `"fdef"`.
21+
22+
**Example 2:**
23+
24+
**Input:** word = "bcdaaaab"
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
The only substring is `"aaaa"`. Note that we cannot **also** choose `"bcdaaaab"` since it intersects with the other substring.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
35+
* `word` consists only of lowercase English letters.
36+
37+
## Solution
38+
39+
```java
40+
import java.util.Arrays;
41+
42+
public class Solution {
43+
public int maxSubstrings(String s) {
44+
int[] prev = new int[26];
45+
int r = 0;
46+
Arrays.fill(prev, -1);
47+
for (int i = 0; i < s.length(); ++i) {
48+
int j = s.charAt(i) - 'a';
49+
if (prev[j] != -1 && i - prev[j] + 1 >= 4) {
50+
++r;
51+
Arrays.fill(prev, -1);
52+
} else if (prev[j] == -1) {
53+
prev[j] = i;
54+
}
55+
}
56+
return r;
57+
}
58+
}
59+
```
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3558\. Number of Ways to Assign Edge Weights I
5+
6+
Medium
7+
8+
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>.
9+
10+
Initially, all edges have a weight of 0. You must assign each edge a weight of either **1** or **2**.
11+
12+
The **cost** of a path between any two nodes `u` and `v` is the total weight of all edges in the path connecting them.
13+
14+
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**.
15+
16+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
**Note:** Ignore all edges **not** in the path from node 1 to `x`.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-060006.png)
23+
24+
**Input:** edges = \[\[1,2]]
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
* The path from Node 1 to Node 2 consists of one edge (`1 → 2`).
31+
* Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-055820.png)
36+
37+
**Input:** edges = \[\[1,2],[1,3],[3,4],[3,5]]
38+
39+
**Output:** 2
40+
41+
**Explanation:**
42+
43+
* The maximum depth is 2, with nodes 4 and 5 at the same depth. Either node can be selected for processing.
44+
* For example, the path from Node 1 to Node 4 consists of two edges (`1 → 3` and `3 → 4`).
45+
* Assigning weights (1,2) or (2,1) results in an odd cost. Thus, the number of valid assignments is 2.
46+
47+
**Constraints:**
48+
49+
* <code>2 <= n <= 10<sup>5</sup></code>
50+
* `edges.length == n - 1`
51+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>]</code>
52+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
53+
* `edges` represents a valid tree.
54+
55+
## Solution
56+
57+
```java
58+
public class Solution {
59+
private static int mod = (int) 1e9 + 7;
60+
private long[] pow2 = new long[100001];
61+
62+
public int assignEdgeWeights(int[][] edges) {
63+
if (pow2[0] == 0) {
64+
pow2[0] = 1;
65+
for (int i = 1; i < pow2.length; i++) {
66+
pow2[i] = (pow2[i - 1] << 1) % mod;
67+
}
68+
}
69+
int n = edges.length + 1;
70+
int[] adj = new int[n + 1];
71+
int[] degrees = new int[n + 1];
72+
for (int[] edge : edges) {
73+
int u = edge[0];
74+
int v = edge[1];
75+
adj[u] += v;
76+
adj[v] += u;
77+
degrees[u]++;
78+
degrees[v]++;
79+
}
80+
int[] que = new int[n];
81+
int write = 0;
82+
int read = 0;
83+
for (int i = 2; i <= n; ++i) {
84+
if (degrees[i] == 1) {
85+
que[write++] = i;
86+
}
87+
}
88+
int distance = 0;
89+
while (read < write) {
90+
distance++;
91+
int size = write - read;
92+
while (size-- > 0) {
93+
int v = que[read++];
94+
int u = adj[v];
95+
adj[u] -= v;
96+
if (--degrees[u] == 1 && u != 1) {
97+
que[write++] = u;
98+
}
99+
}
100+
}
101+
return (int) pow2[distance - 1];
102+
}
103+
}
104+
```

0 commit comments

Comments
 (0)