Skip to content

Commit 6bbb968

Browse files
authored
feat: add new lc problems (#4429)
1 parent 7381ede commit 6bbb968

21 files changed

+895
-1
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3556.Sum%20of%20Largest%20Prime%20Substrings/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3556. 最大质数子字符串之和](https://leetcode.cn/problems/sum-of-largest-prime-substrings)
10+
11+
[English Version](/solution/3500-3599/3556.Sum%20of%20Largest%20Prime%20Substrings/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p data-end="157" data-start="30">给定一个字符串 <code>s</code>,找出可以由其&nbsp;<strong>子字符串&nbsp;</strong>组成的&nbsp;<strong>3个最大的不同质数&nbsp;</strong>的和。</p>
18+
19+
<p data-end="269" data-start="166">返回这些质数的&nbsp;<strong>总和&nbsp;</strong>,如果少于 3 个不同的质数,则返回&nbsp;<strong>所有&nbsp;</strong>不同质数的和。</p>
20+
21+
<p data-end="269" data-start="166">质数是大于 1 且只有两个因数的自然数:1和它本身。</p>
22+
23+
<p data-end="269" data-start="166"><strong>子字符串&nbsp;</strong>是字符串中的一个连续字符序列。&nbsp;</p>
24+
25+
<p data-end="370" data-is-last-node="" data-is-only-node="" data-start="271"><strong data-end="280" data-start="271">注意:</strong>每个质数即使出现在&nbsp;<strong>多个&nbsp;</strong>子字符串中,也只能计算&nbsp;<strong>一次&nbsp;</strong>。此外,将子字符串转换为整数时,忽略任何前导零。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>输入:</strong> <span class="example-io">s = "12234"</span></p>
33+
34+
<p><strong>输出:</strong> <span class="example-io">1469</span></p>
35+
36+
<p><strong>解释:</strong></p>
37+
38+
<ul>
39+
<li data-end="136" data-start="16">由 <code>"12234"</code> 的子字符串形成的不同质数为 2 ,3 ,23 ,223 和 1223。</li>
40+
<li data-end="226" data-start="137">最大的 3 个质数是 1223、223 和 23。它们的和是 1469。</li>
41+
</ul>
42+
</div>
43+
44+
<p><strong class="example">示例 2:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>输入:</strong> <span class="example-io">s = "111"</span></p>
48+
49+
<p><strong>输出:</strong> <span class="example-io">11</span></p>
50+
51+
<p><strong>解释:</strong></p>
52+
53+
<ul>
54+
<li data-end="339" data-start="244">由 <code>"111"</code> 的子字符串形成的不同质数是 11。</li>
55+
<li data-end="412" data-is-last-node="" data-start="340">由于只有一个质数,所以结果是 11。</li>
56+
</ul>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ul>
64+
<li data-end="39" data-start="18"><code>1 &lt;= s.length &lt;= 10</code></li>
65+
<li data-end="68" data-is-last-node="" data-start="40"><code>s</code> 仅由数字组成。</li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## 解法
71+
72+
<!-- solution:start -->
73+
74+
### 方法一
75+
76+
<!-- tabs:start -->
77+
78+
#### Python3
79+
80+
```python
81+
82+
```
83+
84+
#### Java
85+
86+
```java
87+
88+
```
89+
90+
#### C++
91+
92+
```cpp
93+
94+
```
95+
96+
#### Go
97+
98+
```go
99+
100+
```
101+
102+
<!-- tabs:end -->
103+
104+
<!-- solution:end -->
105+
106+
<!-- problem:end -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3556.Sum%20of%20Largest%20Prime%20Substrings/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3556. Sum of Largest Prime Substrings](https://leetcode.com/problems/sum-of-largest-prime-substrings)
10+
11+
[中文文档](/solution/3500-3599/3556.Sum%20of%20Largest%20Prime%20Substrings/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p data-end="157" data-start="30">Given a string <code>s</code>, find the sum of the <strong>3 largest unique prime numbers</strong> that can be formed using any of its<strong> substrings</strong>.</p>
18+
19+
<p data-end="269" data-start="166">Return the <strong>sum</strong> of the three largest unique prime numbers that can be formed. If fewer than three exist, return the sum of <strong>all</strong> available primes. If no prime numbers can be formed, return 0.</p>
20+
21+
<p data-end="269" data-start="166">A prime number is a natural number greater than 1 with only two factors, 1 and itself.</p>
22+
23+
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
24+
25+
<p data-end="370" data-is-last-node="" data-is-only-node="" data-start="271"><strong data-end="280" data-start="271">Note:</strong> Each prime number should be counted only <strong>once</strong>, even if it appears in <strong>multiple</strong> substrings. Additionally, when converting a substring to an integer, any leading zeros are ignored.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">s = &quot;12234&quot;</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">1469</span></p>
34+
35+
<p><strong>Explanation:</strong></p>
36+
37+
<ul>
38+
<li data-end="136" data-start="16">The unique prime numbers formed from the substrings of <code>&quot;12234&quot;</code> are 2, 3, 23, 223, and 1223.</li>
39+
<li data-end="226" data-start="137">The 3 largest primes are 1223, 223, and 23. Their sum is 1469.</li>
40+
</ul>
41+
</div>
42+
43+
<p><strong class="example">Example 2:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">s = &quot;111&quot;</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">11</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
52+
<ul>
53+
<li data-end="339" data-start="244">The unique prime number formed from the substrings of <code>&quot;111&quot;</code> is 11.</li>
54+
<li data-end="412" data-is-last-node="" data-start="340">Since there is only one prime number, the sum is 11.</li>
55+
</ul>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li data-end="39" data-start="18"><code>1 &lt;= s.length &lt;= 10</code></li>
63+
<li data-end="68" data-is-last-node="" data-start="40"><code>s</code> consists of only digits.</li>
64+
</ul>
65+
66+
<!-- description:end -->
67+
68+
## Solutions
69+
70+
<!-- solution:start -->
71+
72+
### Solution 1
73+
74+
<!-- tabs:start -->
75+
76+
#### Python3
77+
78+
```python
79+
80+
```
81+
82+
#### Java
83+
84+
```java
85+
86+
```
87+
88+
#### C++
89+
90+
```cpp
91+
92+
```
93+
94+
#### Go
95+
96+
```go
97+
98+
```
99+
100+
<!-- tabs:end -->
101+
102+
<!-- solution:end -->
103+
104+
<!-- problem:end -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3557.Find%20Maximum%20Number%20of%20Non%20Intersecting%20Substrings/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3557. 不相交子字符串的最大数量](https://leetcode.cn/problems/find-maximum-number-of-non-intersecting-substrings)
10+
11+
[English Version](/solution/3500-3599/3557.Find%20Maximum%20Number%20of%20Non%20Intersecting%20Substrings/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个字符串 <code>word</code>。</p>
18+
19+
<p>返回以&nbsp;<strong>首尾字母相同&nbsp;</strong>且&nbsp;<strong>长度至少为 4&nbsp;</strong>的&nbsp;<strong>不相交子字符串&nbsp;</strong>的最大数量。</p>
20+
21+
<p><strong>子字符串&nbsp;</strong>是字符串中连续的&nbsp;<b>非空&nbsp;</b>字符序列。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">word = "abcdeafdef"</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>两个子字符串是 <code>"abcdea"</code> 和 <code>"fdef"</code>。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">word = "bcdaaaab"</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>唯一的子字符串是 <code>"aaaa"</code>。注意我们&nbsp;<strong>不能&nbsp;</strong>同时选择 <code>"bcdaaaab"</code>,因为它和另一个子字符串有重叠。</p>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= word.length &lt;= 2 * 10<sup>5</sup></code></li>
55+
<li><code>word</code> 仅由小写英文字母组成。</li>
56+
</ul>
57+
58+
<!-- description:end -->
59+
60+
## 解法
61+
62+
<!-- solution:start -->
63+
64+
### 方法一
65+
66+
<!-- tabs:start -->
67+
68+
#### Python3
69+
70+
```python
71+
72+
```
73+
74+
#### Java
75+
76+
```java
77+
78+
```
79+
80+
#### C++
81+
82+
```cpp
83+
84+
```
85+
86+
#### Go
87+
88+
```go
89+
90+
```
91+
92+
<!-- tabs:end -->
93+
94+
<!-- solution:end -->
95+
96+
<!-- problem:end -->

0 commit comments

Comments
 (0)