Skip to content

Commit 9d57183

Browse files
committed
feat: add solutions to lc problem: No.1781
No.1781.Sum of Beauty of All Substrings
1 parent 0dbc515 commit 9d57183

File tree

8 files changed

+146
-118
lines changed

8 files changed

+146
-118
lines changed

solution/0900-0999/0912.Sort an Array/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@
4242

4343
**方法一:快速排序**
4444

45+
快速排序是一种高效的排序算法。它的基本思想是通过一趟排序将待排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
46+
47+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
48+
4549
**方法二:归并排序**
4650

47-
以上两种方法时间复杂度均为 O(nlogn)。
51+
归并排序是一种分治算法,其思想是将待排序的数据序列不断地折半拆分,直到每个数据块只有一个元素为止,然后再按照拆分的顺序将每个数据块两两合并,在合并的过程中进行排序,最终得到一个有序的数据序列。
52+
53+
归并排序是一种稳定的排序算法,时间复杂度为 $O(n \times \log n)$,空间复杂度为 $O(n)$。其中 $n$ 为数组长度。
4854

4955
<!-- tabs:start -->
5056

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

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
计数器实现。`s[i, j]` 的 counter 可用于计算 `s[i, j + 1]`
46+
**方法一:枚举 + 计数**
47+
48+
枚举每个子串的起点,找到以该起点为左端点的所有子串,然后计算每个子串的美丽值,最后将所有子串的美丽值相加即可。
49+
50+
时间复杂度 O(n^2 \times C),空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
4751

4852
<!-- tabs:start -->
4953

@@ -56,11 +60,10 @@ class Solution:
5660
def beautySum(self, s: str) -> int:
5761
ans, n = 0, len(s)
5862
for i in range(n):
59-
counter = Counter()
63+
cnt = Counter()
6064
for j in range(i, n):
61-
counter[s[j]] += 1
62-
t = [v for v in counter.values() if v]
63-
ans += max(t) - min(t)
65+
cnt[s[j]] += 1
66+
ans += max(cnt.values()) - min(cnt.values())
6467
return ans
6568
```
6669

@@ -70,17 +73,15 @@ class Solution:
7073

7174
```java
7275
class Solution {
73-
7476
public int beautySum(String s) {
7577
int ans = 0;
7678
int n = s.length();
7779
for (int i = 0; i < n; ++i) {
78-
int[] counter = new int[26];
80+
int[] cnt = new int[26];
7981
for (int j = i; j < n; ++j) {
80-
++counter[s.charAt(j) - 'a'];
81-
int mi = 1000;
82-
int mx = 0;
83-
for (int v : counter) {
82+
++cnt[s.charAt(j) - 'a'];
83+
int mi = 1000, mx = 0;
84+
for (int v : cnt) {
8485
if (v > 0) {
8586
mi = Math.min(mi, v);
8687
mx = Math.max(mx, v);
@@ -102,14 +103,14 @@ public:
102103
int beautySum(string s) {
103104
int ans = 0;
104105
int n = s.size();
106+
int cnt[26];
105107
for (int i = 0; i < n; ++i) {
106-
vector<int> counter(26);
108+
memset(cnt, 0, sizeof cnt);
107109
for (int j = i; j < n; ++j) {
108-
++counter[s[j] - 'a'];
109-
int mi = 1000;
110-
int mx = 0;
111-
for (int v : counter) {
112-
if (v) {
110+
++cnt[s[j] - 'a'];
111+
int mi = 1000, mx = 0;
112+
for (int& v : cnt) {
113+
if (v > 0) {
113114
mi = min(mi, v);
114115
mx = max(mx, v);
115116
}
@@ -125,38 +126,48 @@ public:
125126
### **Go**
126127
127128
```go
128-
func beautySum(s string) int {
129-
ans, n := 0, len(s)
130-
for i := 0; i < n; i++ {
131-
counter := make([]int, 26)
132-
for j := i; j < n; j++ {
133-
counter[s[j]-'a']++
129+
func beautySum(s string) (ans int) {
130+
for i := range s {
131+
cnt := [26]int{}
132+
for j := i; j < len(s); j++ {
133+
cnt[s[j]-'a']++
134134
mi, mx := 1000, 0
135-
for _, v := range counter {
135+
for _, v := range cnt {
136136
if v > 0 {
137-
mi = min(mi, v)
138-
mx = max(mx, v)
137+
if mi > v {
138+
mi = v
139+
}
140+
if mx < v {
141+
mx = v
142+
}
139143
}
140144
}
141145
ans += mx - mi
142146
}
143147
}
144-
return ans
145-
}
146-
147-
func max(a, b int) int {
148-
if a > b {
149-
return a
150-
}
151-
return b
148+
return
152149
}
150+
```
153151

154-
func min(a, b int) int {
155-
if a < b {
156-
return a
157-
}
158-
return b
159-
}
152+
### **JavaScript**
153+
154+
```js
155+
/**
156+
* @param {string} s
157+
* @return {number}
158+
*/
159+
var beautySum = function (s) {
160+
let ans = 0;
161+
for (let i = 0; i < s.length; ++i) {
162+
const cnt = new Map();
163+
for (let j = i; j < s.length; ++j) {
164+
cnt.set(s[j], (cnt.get(s[j]) || 0) + 1);
165+
const t = Array.from(cnt.values());
166+
ans += Math.max(...t) - Math.min(...t);
167+
}
168+
}
169+
return ans;
170+
};
160171
```
161172

162173
### **...**

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ class Solution:
4646
def beautySum(self, s: str) -> int:
4747
ans, n = 0, len(s)
4848
for i in range(n):
49-
counter = Counter()
49+
cnt = Counter()
5050
for j in range(i, n):
51-
counter[s[j]] += 1
52-
t = [v for v in counter.values() if v]
53-
ans += max(t) - min(t)
51+
cnt[s[j]] += 1
52+
ans += max(cnt.values()) - min(cnt.values())
5453
return ans
5554
```
5655

@@ -62,12 +61,11 @@ class Solution {
6261
int ans = 0;
6362
int n = s.length();
6463
for (int i = 0; i < n; ++i) {
65-
int[] counter = new int[26];
64+
int[] cnt = new int[26];
6665
for (int j = i; j < n; ++j) {
67-
++counter[s.charAt(j) - 'a'];
68-
int mi = 1000;
69-
int mx = 0;
70-
for (int v : counter) {
66+
++cnt[s.charAt(j) - 'a'];
67+
int mi = 1000, mx = 0;
68+
for (int v : cnt) {
7169
if (v > 0) {
7270
mi = Math.min(mi, v);
7371
mx = Math.max(mx, v);
@@ -89,14 +87,14 @@ public:
8987
int beautySum(string s) {
9088
int ans = 0;
9189
int n = s.size();
90+
int cnt[26];
9291
for (int i = 0; i < n; ++i) {
93-
vector<int> counter(26);
92+
memset(cnt, 0, sizeof cnt);
9493
for (int j = i; j < n; ++j) {
95-
++counter[s[j] - 'a'];
96-
int mi = 1000;
97-
int mx = 0;
98-
for (int v : counter) {
99-
if (v) {
94+
++cnt[s[j] - 'a'];
95+
int mi = 1000, mx = 0;
96+
for (int& v : cnt) {
97+
if (v > 0) {
10098
mi = min(mi, v);
10199
mx = max(mx, v);
102100
}
@@ -112,38 +110,48 @@ public:
112110
### **Go**
113111
114112
```go
115-
func beautySum(s string) int {
116-
ans, n := 0, len(s)
117-
for i := 0; i < n; i++ {
118-
counter := make([]int, 26)
119-
for j := i; j < n; j++ {
120-
counter[s[j]-'a']++
113+
func beautySum(s string) (ans int) {
114+
for i := range s {
115+
cnt := [26]int{}
116+
for j := i; j < len(s); j++ {
117+
cnt[s[j]-'a']++
121118
mi, mx := 1000, 0
122-
for _, v := range counter {
119+
for _, v := range cnt {
123120
if v > 0 {
124-
mi = min(mi, v)
125-
mx = max(mx, v)
121+
if mi > v {
122+
mi = v
123+
}
124+
if mx < v {
125+
mx = v
126+
}
126127
}
127128
}
128129
ans += mx - mi
129130
}
130131
}
131-
return ans
132-
}
133-
134-
func max(a, b int) int {
135-
if a > b {
136-
return a
137-
}
138-
return b
132+
return
139133
}
134+
```
140135

141-
func min(a, b int) int {
142-
if a < b {
143-
return a
144-
}
145-
return b
146-
}
136+
### **JavaScript**
137+
138+
```js
139+
/**
140+
* @param {string} s
141+
* @return {number}
142+
*/
143+
var beautySum = function (s) {
144+
let ans = 0;
145+
for (let i = 0; i < s.length; ++i) {
146+
const cnt = new Map();
147+
for (let j = i; j < s.length; ++j) {
148+
cnt.set(s[j], (cnt.get(s[j]) || 0) + 1);
149+
const t = Array.from(cnt.values());
150+
ans += Math.max(...t) - Math.min(...t);
151+
}
152+
}
153+
return ans;
154+
};
147155
```
148156

149157
### **...**

solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ class Solution {
33
int beautySum(string s) {
44
int ans = 0;
55
int n = s.size();
6+
int cnt[26];
67
for (int i = 0; i < n; ++i) {
7-
vector<int> counter(26);
8+
memset(cnt, 0, sizeof cnt);
89
for (int j = i; j < n; ++j) {
9-
++counter[s[j] - 'a'];
10-
int mi = 1000;
11-
int mx = 0;
12-
for (int v : counter) {
13-
if (v) {
10+
++cnt[s[j] - 'a'];
11+
int mi = 1000, mx = 0;
12+
for (int& v : cnt) {
13+
if (v > 0) {
1414
mi = min(mi, v);
1515
mx = max(mx, v);
1616
}
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
func beautySum(s string) int {
2-
ans, n := 0, len(s)
3-
for i := 0; i < n; i++ {
4-
counter := make([]int, 26)
5-
for j := i; j < n; j++ {
6-
counter[s[j]-'a']++
1+
func beautySum(s string) (ans int) {
2+
for i := range s {
3+
cnt := [26]int{}
4+
for j := i; j < len(s); j++ {
5+
cnt[s[j]-'a']++
76
mi, mx := 1000, 0
8-
for _, v := range counter {
7+
for _, v := range cnt {
98
if v > 0 {
10-
mi = min(mi, v)
11-
mx = max(mx, v)
9+
if mi > v {
10+
mi = v
11+
}
12+
if mx < v {
13+
mx = v
14+
}
1215
}
1316
}
1417
ans += mx - mi
1518
}
1619
}
17-
return ans
18-
}
19-
20-
func max(a, b int) int {
21-
if a > b {
22-
return a
23-
}
24-
return b
25-
}
26-
27-
func min(a, b int) int {
28-
if a < b {
29-
return a
30-
}
31-
return b
20+
return
3221
}

solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ public int beautySum(String s) {
33
int ans = 0;
44
int n = s.length();
55
for (int i = 0; i < n; ++i) {
6-
int[] counter = new int[26];
6+
int[] cnt = new int[26];
77
for (int j = i; j < n; ++j) {
8-
++counter[s.charAt(j) - 'a'];
9-
int mi = 1000;
10-
int mx = 0;
11-
for (int v : counter) {
8+
++cnt[s.charAt(j) - 'a'];
9+
int mi = 1000, mx = 0;
10+
for (int v : cnt) {
1211
if (v > 0) {
1312
mi = Math.min(mi, v);
1413
mx = Math.max(mx, v);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var beautySum = function (s) {
6+
let ans = 0;
7+
for (let i = 0; i < s.length; ++i) {
8+
const cnt = new Map();
9+
for (let j = i; j < s.length; ++j) {
10+
cnt.set(s[j], (cnt.get(s[j]) || 0) + 1);
11+
const t = Array.from(cnt.values());
12+
ans += Math.max(...t) - Math.min(...t);
13+
}
14+
}
15+
return ans;
16+
};

solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ class Solution:
22
def beautySum(self, s: str) -> int:
33
ans, n = 0, len(s)
44
for i in range(n):
5-
counter = Counter()
5+
cnt = Counter()
66
for j in range(i, n):
7-
counter[s[j]] += 1
8-
t = [v for v in counter.values() if v]
9-
ans += max(t) - min(t)
7+
cnt[s[j]] += 1
8+
ans += max(cnt.values()) - min(cnt.values())
109
return ans

0 commit comments

Comments
 (0)