Skip to content

Commit b890898

Browse files
authored
feat: add solutions to lc problem: No.3556 (#4430)
1 parent 6bbb968 commit b890898

File tree

7 files changed

+473
-8
lines changed

7 files changed

+473
-8
lines changed

solution/3500-3599/3556.Sum of Largest Prime Substrings/README.md

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,190 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3556.Su
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:枚举 + 哈希表
75+
76+
我们可以枚举所有的子字符串,然后判断它们是否是质数。由于题目要求我们返回最大的 3 个不同质数的和,因此我们可以使用一个哈希表来存储所有的质数。
77+
78+
在遍历完所有的子字符串后,我们将哈希表中的质数按从小到大的顺序排序,然后取出最大的 3 个质数进行求和。
79+
80+
如果哈希表中质数的数量小于 3,则返回所有质数的和。
81+
82+
时间复杂度 $O(n^2 \times \sqrt{M})$,空间复杂度 $O(n^2)$,其中 $n$ 为字符串的长度,而 $M$ 为字符串中最大的子字符串的值。
7583

7684
<!-- tabs:start -->
7785

7886
#### Python3
7987

8088
```python
81-
89+
class Solution:
90+
def sumOfLargestPrimes(self, s: str) -> int:
91+
def is_prime(x: int) -> bool:
92+
if x < 2:
93+
return False
94+
return all(x % i for i in range(2, int(sqrt(x)) + 1))
95+
96+
st = set()
97+
n = len(s)
98+
for i in range(n):
99+
x = 0
100+
for j in range(i, n):
101+
x = x * 10 + int(s[j])
102+
if is_prime(x):
103+
st.add(x)
104+
return sum(sorted(st)[-3:])
82105
```
83106

84107
#### Java
85108

86109
```java
87-
110+
class Solution {
111+
public long sumOfLargestPrimes(String s) {
112+
Set<Long> st = new HashSet<>();
113+
int n = s.length();
114+
115+
for (int i = 0; i < n; i++) {
116+
long x = 0;
117+
for (int j = i; j < n; j++) {
118+
x = x * 10 + (s.charAt(j) - '0');
119+
if (is_prime(x)) {
120+
st.add(x);
121+
}
122+
}
123+
}
124+
125+
List<Long> sorted = new ArrayList<>(st);
126+
Collections.sort(sorted);
127+
128+
long ans = 0;
129+
int start = Math.max(0, sorted.size() - 3);
130+
for (int idx = start; idx < sorted.size(); idx++) {
131+
ans += sorted.get(idx);
132+
}
133+
return ans;
134+
}
135+
136+
private boolean is_prime(long x) {
137+
if (x < 2) return false;
138+
for (long i = 2; i * i <= x; i++) {
139+
if (x % i == 0) return false;
140+
}
141+
return true;
142+
}
143+
}
88144
```
89145

90146
#### C++
91147

92148
```cpp
93-
149+
class Solution {
150+
public:
151+
long long sumOfLargestPrimes(string s) {
152+
unordered_set<long long> st;
153+
int n = s.size();
154+
155+
for (int i = 0; i < n; ++i) {
156+
long long x = 0;
157+
for (int j = i; j < n; ++j) {
158+
x = x * 10 + (s[j] - '0');
159+
if (is_prime(x)) {
160+
st.insert(x);
161+
}
162+
}
163+
}
164+
165+
vector<long long> sorted(st.begin(), st.end());
166+
sort(sorted.begin(), sorted.end());
167+
168+
long long ans = 0;
169+
int cnt = 0;
170+
for (int i = (int) sorted.size() - 1; i >= 0 && cnt < 3; --i, ++cnt) {
171+
ans += sorted[i];
172+
}
173+
return ans;
174+
}
175+
176+
private:
177+
bool is_prime(long long x) {
178+
if (x < 2) return false;
179+
for (long long i = 2; i * i <= x; ++i) {
180+
if (x % i == 0) return false;
181+
}
182+
return true;
183+
}
184+
};
94185
```
95186
96187
#### Go
97188
98189
```go
190+
func sumOfLargestPrimes(s string) (ans int64) {
191+
st := make(map[int64]struct{})
192+
n := len(s)
193+
194+
for i := 0; i < n; i++ {
195+
var x int64 = 0
196+
for j := i; j < n; j++ {
197+
x = x*10 + int64(s[j]-'0')
198+
if isPrime(x) {
199+
st[x] = struct{}{}
200+
}
201+
}
202+
}
203+
204+
nums := make([]int64, 0, len(st))
205+
for num := range st {
206+
nums = append(nums, num)
207+
}
208+
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
209+
for i := len(nums) - 1; i >= 0 && len(nums)-i <= 3; i-- {
210+
ans += nums[i]
211+
}
212+
return
213+
}
214+
215+
func isPrime(x int64) bool {
216+
if x < 2 {
217+
return false
218+
}
219+
sqrtX := int64(math.Sqrt(float64(x)))
220+
for i := int64(2); i <= sqrtX; i++ {
221+
if x%i == 0 {
222+
return false
223+
}
224+
}
225+
return true
226+
}
227+
```
99228

229+
#### TypeScript
230+
231+
```ts
232+
function sumOfLargestPrimes(s: string): number {
233+
const st = new Set<number>();
234+
const n = s.length;
235+
236+
for (let i = 0; i < n; i++) {
237+
let x = 0;
238+
for (let j = i; j < n; j++) {
239+
x = x * 10 + Number(s[j]);
240+
if (isPrime(x)) {
241+
st.add(x);
242+
}
243+
}
244+
}
245+
246+
const sorted = Array.from(st).sort((a, b) => a - b);
247+
const topThree = sorted.slice(-3);
248+
return topThree.reduce((sum, val) => sum + val, 0);
249+
}
250+
251+
function isPrime(x: number): boolean {
252+
if (x < 2) return false;
253+
for (let i = 2; i * i <= x; i++) {
254+
if (x % i === 0) return false;
255+
}
256+
return true;
257+
}
100258
```
101259

102260
<!-- tabs:end -->

solution/3500-3599/3556.Sum of Largest Prime Substrings/README_EN.md

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,190 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3556.Su
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Enumeration + Hash Table
73+
74+
We can enumerate all substrings and check whether they are prime numbers. Since the problem requires us to return the sum of the largest 3 distinct primes, we can use a hash table to store all the primes.
75+
76+
After traversing all substrings, we sort the primes in the hash table in ascending order, and then take the largest 3 primes to calculate the sum.
77+
78+
If there are fewer than 3 primes in the hash table, return the sum of all primes.
79+
80+
The time complexity is $O(n^2 \times \sqrt{M})$, and the space complexity is $O(n^2)$, where $n$ is the length of the string and $M$ is the value of the largest substring.
7381

7482
<!-- tabs:start -->
7583

7684
#### Python3
7785

7886
```python
79-
87+
class Solution:
88+
def sumOfLargestPrimes(self, s: str) -> int:
89+
def is_prime(x: int) -> bool:
90+
if x < 2:
91+
return False
92+
return all(x % i for i in range(2, int(sqrt(x)) + 1))
93+
94+
st = set()
95+
n = len(s)
96+
for i in range(n):
97+
x = 0
98+
for j in range(i, n):
99+
x = x * 10 + int(s[j])
100+
if is_prime(x):
101+
st.add(x)
102+
return sum(sorted(st)[-3:])
80103
```
81104

82105
#### Java
83106

84107
```java
85-
108+
class Solution {
109+
public long sumOfLargestPrimes(String s) {
110+
Set<Long> st = new HashSet<>();
111+
int n = s.length();
112+
113+
for (int i = 0; i < n; i++) {
114+
long x = 0;
115+
for (int j = i; j < n; j++) {
116+
x = x * 10 + (s.charAt(j) - '0');
117+
if (is_prime(x)) {
118+
st.add(x);
119+
}
120+
}
121+
}
122+
123+
List<Long> sorted = new ArrayList<>(st);
124+
Collections.sort(sorted);
125+
126+
long ans = 0;
127+
int start = Math.max(0, sorted.size() - 3);
128+
for (int idx = start; idx < sorted.size(); idx++) {
129+
ans += sorted.get(idx);
130+
}
131+
return ans;
132+
}
133+
134+
private boolean is_prime(long x) {
135+
if (x < 2) return false;
136+
for (long i = 2; i * i <= x; i++) {
137+
if (x % i == 0) return false;
138+
}
139+
return true;
140+
}
141+
}
86142
```
87143

88144
#### C++
89145

90146
```cpp
91-
147+
class Solution {
148+
public:
149+
long long sumOfLargestPrimes(string s) {
150+
unordered_set<long long> st;
151+
int n = s.size();
152+
153+
for (int i = 0; i < n; ++i) {
154+
long long x = 0;
155+
for (int j = i; j < n; ++j) {
156+
x = x * 10 + (s[j] - '0');
157+
if (is_prime(x)) {
158+
st.insert(x);
159+
}
160+
}
161+
}
162+
163+
vector<long long> sorted(st.begin(), st.end());
164+
sort(sorted.begin(), sorted.end());
165+
166+
long long ans = 0;
167+
int cnt = 0;
168+
for (int i = (int) sorted.size() - 1; i >= 0 && cnt < 3; --i, ++cnt) {
169+
ans += sorted[i];
170+
}
171+
return ans;
172+
}
173+
174+
private:
175+
bool is_prime(long long x) {
176+
if (x < 2) return false;
177+
for (long long i = 2; i * i <= x; ++i) {
178+
if (x % i == 0) return false;
179+
}
180+
return true;
181+
}
182+
};
92183
```
93184
94185
#### Go
95186
96187
```go
188+
func sumOfLargestPrimes(s string) (ans int64) {
189+
st := make(map[int64]struct{})
190+
n := len(s)
191+
192+
for i := 0; i < n; i++ {
193+
var x int64 = 0
194+
for j := i; j < n; j++ {
195+
x = x*10 + int64(s[j]-'0')
196+
if isPrime(x) {
197+
st[x] = struct{}{}
198+
}
199+
}
200+
}
201+
202+
nums := make([]int64, 0, len(st))
203+
for num := range st {
204+
nums = append(nums, num)
205+
}
206+
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
207+
for i := len(nums) - 1; i >= 0 && len(nums)-i <= 3; i-- {
208+
ans += nums[i]
209+
}
210+
return
211+
}
212+
213+
func isPrime(x int64) bool {
214+
if x < 2 {
215+
return false
216+
}
217+
sqrtX := int64(math.Sqrt(float64(x)))
218+
for i := int64(2); i <= sqrtX; i++ {
219+
if x%i == 0 {
220+
return false
221+
}
222+
}
223+
return true
224+
}
225+
```
97226

227+
#### TypeScript
228+
229+
```ts
230+
function sumOfLargestPrimes(s: string): number {
231+
const st = new Set<number>();
232+
const n = s.length;
233+
234+
for (let i = 0; i < n; i++) {
235+
let x = 0;
236+
for (let j = i; j < n; j++) {
237+
x = x * 10 + Number(s[j]);
238+
if (isPrime(x)) {
239+
st.add(x);
240+
}
241+
}
242+
}
243+
244+
const sorted = Array.from(st).sort((a, b) => a - b);
245+
const topThree = sorted.slice(-3);
246+
return topThree.reduce((sum, val) => sum + val, 0);
247+
}
248+
249+
function isPrime(x: number): boolean {
250+
if (x < 2) return false;
251+
for (let i = 2; i * i <= x; i++) {
252+
if (x % i === 0) return false;
253+
}
254+
return true;
255+
}
98256
```
99257

100258
<!-- tabs:end -->

0 commit comments

Comments
 (0)