Skip to content

Commit f3b01f8

Browse files
authoredDec 19, 2023
feat: add solutions to lc problems: No.1781+ (doocs#2125)
1 parent fe9bacc commit f3b01f8

File tree

6 files changed

+332
-2
lines changed

6 files changed

+332
-2
lines changed
 

‎solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md

+151
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ class Solution:
6767
return ans
6868
```
6969

70+
```python
71+
class Solution:
72+
def beautySum(self, s: str) -> int:
73+
ans, n = 0, len(s)
74+
for i in range(n):
75+
cnt = Counter()
76+
freq = Counter()
77+
mi = mx = 1
78+
for j in range(i, n):
79+
freq[cnt[s[j]]] -= 1
80+
cnt[s[j]] += 1
81+
freq[cnt[s[j]]] += 1
82+
83+
if cnt[s[j]] == 1:
84+
mi = 1
85+
if freq[mi] == 0:
86+
mi += 1
87+
if cnt[s[j]] > mx:
88+
mx = cnt[s[j]]
89+
90+
ans += mx - mi
91+
return ans
92+
```
93+
7094
### **Java**
7195

7296
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -95,6 +119,38 @@ class Solution {
95119
}
96120
```
97121

122+
```java
123+
class Solution {
124+
public int beautySum(String s) {
125+
int n = s.length();
126+
int ans = 0;
127+
for (int i = 0; i < n; ++i) {
128+
int[] cnt = new int[26];
129+
Map<Integer, Integer> freq = new HashMap<>();
130+
int mi = 1, mx = 1;
131+
for (int j = i; j < n; ++j) {
132+
int k = s.charAt(j) - 'a';
133+
freq.merge(cnt[k], -1, Integer::sum);
134+
++cnt[k];
135+
freq.merge(cnt[k], 1, Integer::sum);
136+
137+
if (cnt[k] == 1) {
138+
mi = 1;
139+
}
140+
if (freq.getOrDefault(mi, 0) == 0) {
141+
++mi;
142+
}
143+
if (cnt[k] > mx) {
144+
mx = cnt[k];
145+
}
146+
ans += mx - mi;
147+
}
148+
}
149+
return ans;
150+
}
151+
}
152+
```
153+
98154
### **C++**
99155

100156
```cpp
@@ -123,6 +179,39 @@ public:
123179
};
124180
```
125181
182+
```cpp
183+
class Solution {
184+
public:
185+
int beautySum(string s) {
186+
int n = s.size();
187+
int ans = 0;
188+
for (int i = 0; i < n; ++i) {
189+
int cnt[26]{};
190+
unordered_map<int, int> freq;
191+
int mi = 1, mx = 1;
192+
for (int j = i; j < n; ++j) {
193+
int k = s[j] - 'a';
194+
--freq[cnt[k]];
195+
++cnt[k];
196+
++freq[cnt[k]];
197+
198+
if (cnt[k] == 1) {
199+
mi = 1;
200+
}
201+
if (freq[mi] == 0) {
202+
++mi;
203+
}
204+
if (cnt[k] > mx) {
205+
mx = cnt[k];
206+
}
207+
ans += mx - mi;
208+
}
209+
}
210+
return ans;
211+
}
212+
};
213+
```
214+
126215
### **Go**
127216

128217
```go
@@ -149,6 +238,35 @@ func beautySum(s string) (ans int) {
149238
}
150239
```
151240

241+
```go
242+
func beautySum(s string) (ans int) {
243+
n := len(s)
244+
for i := 0; i < n; i++ {
245+
cnt := [26]int{}
246+
freq := map[int]int{}
247+
mi, mx := 1, 1
248+
for j := i; j < n; j++ {
249+
k := int(s[j] - 'a')
250+
freq[cnt[k]]--
251+
cnt[k]++
252+
freq[cnt[k]]++
253+
254+
if cnt[k] == 1 {
255+
mi = 1
256+
}
257+
if freq[mi] == 0 {
258+
mi++
259+
}
260+
if cnt[k] > mx {
261+
mx = cnt[k]
262+
}
263+
ans += mx - mi
264+
}
265+
}
266+
return
267+
}
268+
```
269+
152270
### **JavaScript**
153271

154272
```js
@@ -170,6 +288,39 @@ var beautySum = function (s) {
170288
};
171289
```
172290

291+
```js
292+
/**
293+
* @param {string} s
294+
* @return {number}
295+
*/
296+
var beautySum = function (s) {
297+
const n = s.length;
298+
let ans = 0;
299+
for (let i = 0; i < n; ++i) {
300+
const cnt = Array(26).fill(0);
301+
const freq = new Map();
302+
let [mi, mx] = [1, 1];
303+
for (let j = i; j < n; ++j) {
304+
const k = s[j].charCodeAt() - 97;
305+
freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1);
306+
++cnt[k];
307+
freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1);
308+
if (cnt[k] === 1) {
309+
mi = 1;
310+
}
311+
if (freq.get(mi) === 0) {
312+
++mi;
313+
}
314+
if (cnt[k] > mx) {
315+
mx = cnt[k];
316+
}
317+
ans += mx - mi;
318+
}
319+
}
320+
return ans;
321+
};
322+
```
323+
173324
### **...**
174325

175326
```

‎solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md

+157
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Enumeration + Counting**
41+
42+
Enumerate the starting position $i$ of each substring, find all substrings with the character at this starting position as the left endpoint, then calculate the beauty value of each substring, and accumulate it to the answer.
43+
44+
The time complexity is $O(n^2 \times C)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, $C = 26$.
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**
@@ -53,6 +59,30 @@ class Solution:
5359
return ans
5460
```
5561

62+
```python
63+
class Solution:
64+
def beautySum(self, s: str) -> int:
65+
ans, n = 0, len(s)
66+
for i in range(n):
67+
cnt = Counter()
68+
freq = Counter()
69+
mi = mx = 1
70+
for j in range(i, n):
71+
freq[cnt[s[j]]] -= 1
72+
cnt[s[j]] += 1
73+
freq[cnt[s[j]]] += 1
74+
75+
if cnt[s[j]] == 1:
76+
mi = 1
77+
if freq[mi] == 0:
78+
mi += 1
79+
if cnt[s[j]] > mx:
80+
mx = cnt[s[j]]
81+
82+
ans += mx - mi
83+
return ans
84+
```
85+
5686
### **Java**
5787

5888
```java
@@ -79,6 +109,38 @@ class Solution {
79109
}
80110
```
81111

112+
```java
113+
class Solution {
114+
public int beautySum(String s) {
115+
int n = s.length();
116+
int ans = 0;
117+
for (int i = 0; i < n; ++i) {
118+
int[] cnt = new int[26];
119+
Map<Integer, Integer> freq = new HashMap<>();
120+
int mi = 1, mx = 1;
121+
for (int j = i; j < n; ++j) {
122+
int k = s.charAt(j) - 'a';
123+
freq.merge(cnt[k], -1, Integer::sum);
124+
++cnt[k];
125+
freq.merge(cnt[k], 1, Integer::sum);
126+
127+
if (cnt[k] == 1) {
128+
mi = 1;
129+
}
130+
if (freq.getOrDefault(mi, 0) == 0) {
131+
++mi;
132+
}
133+
if (cnt[k] > mx) {
134+
mx = cnt[k];
135+
}
136+
ans += mx - mi;
137+
}
138+
}
139+
return ans;
140+
}
141+
}
142+
```
143+
82144
### **C++**
83145

84146
```cpp
@@ -107,6 +169,39 @@ public:
107169
};
108170
```
109171
172+
```cpp
173+
class Solution {
174+
public:
175+
int beautySum(string s) {
176+
int n = s.size();
177+
int ans = 0;
178+
for (int i = 0; i < n; ++i) {
179+
int cnt[26]{};
180+
unordered_map<int, int> freq;
181+
int mi = 1, mx = 1;
182+
for (int j = i; j < n; ++j) {
183+
int k = s[j] - 'a';
184+
--freq[cnt[k]];
185+
++cnt[k];
186+
++freq[cnt[k]];
187+
188+
if (cnt[k] == 1) {
189+
mi = 1;
190+
}
191+
if (freq[mi] == 0) {
192+
++mi;
193+
}
194+
if (cnt[k] > mx) {
195+
mx = cnt[k];
196+
}
197+
ans += mx - mi;
198+
}
199+
}
200+
return ans;
201+
}
202+
};
203+
```
204+
110205
### **Go**
111206

112207
```go
@@ -133,6 +228,35 @@ func beautySum(s string) (ans int) {
133228
}
134229
```
135230

231+
```go
232+
func beautySum(s string) (ans int) {
233+
n := len(s)
234+
for i := 0; i < n; i++ {
235+
cnt := [26]int{}
236+
freq := map[int]int{}
237+
mi, mx := 1, 1
238+
for j := i; j < n; j++ {
239+
k := int(s[j] - 'a')
240+
freq[cnt[k]]--
241+
cnt[k]++
242+
freq[cnt[k]]++
243+
244+
if cnt[k] == 1 {
245+
mi = 1
246+
}
247+
if freq[mi] == 0 {
248+
mi++
249+
}
250+
if cnt[k] > mx {
251+
mx = cnt[k]
252+
}
253+
ans += mx - mi
254+
}
255+
}
256+
return
257+
}
258+
```
259+
136260
### **JavaScript**
137261

138262
```js
@@ -154,6 +278,39 @@ var beautySum = function (s) {
154278
};
155279
```
156280

281+
```js
282+
/**
283+
* @param {string} s
284+
* @return {number}
285+
*/
286+
var beautySum = function (s) {
287+
const n = s.length;
288+
let ans = 0;
289+
for (let i = 0; i < n; ++i) {
290+
const cnt = Array(26).fill(0);
291+
const freq = new Map();
292+
let [mi, mx] = [1, 1];
293+
for (let j = i; j < n; ++j) {
294+
const k = s[j].charCodeAt() - 97;
295+
freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1);
296+
++cnt[k];
297+
freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1);
298+
if (cnt[k] === 1) {
299+
mi = 1;
300+
}
301+
if (freq.get(mi) === 0) {
302+
++mi;
303+
}
304+
if (cnt[k] > mx) {
305+
mx = cnt[k];
306+
}
307+
ans += mx - mi;
308+
}
309+
}
310+
return ans;
311+
};
312+
```
313+
157314
### **...**
158315

159316
```

‎solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
因此,只需要判断字符串 $s$ 是否存在 "01" 串即可。
5252

53-
时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
53+
时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
5454

5555
<!-- tabs:start -->
5656

0 commit comments

Comments
 (0)