Skip to content

feat: add solutions to lc problems: No.1781+ #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ class Solution:
return ans
```

```python
class Solution:
def beautySum(self, s: str) -> int:
ans, n = 0, len(s)
for i in range(n):
cnt = Counter()
freq = Counter()
mi = mx = 1
for j in range(i, n):
freq[cnt[s[j]]] -= 1
cnt[s[j]] += 1
freq[cnt[s[j]]] += 1

if cnt[s[j]] == 1:
mi = 1
if freq[mi] == 0:
mi += 1
if cnt[s[j]] > mx:
mx = cnt[s[j]]

ans += mx - mi
return ans
```

### **Java**

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

```java
class Solution {
public int beautySum(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; ++i) {
int[] cnt = new int[26];
Map<Integer, Integer> freq = new HashMap<>();
int mi = 1, mx = 1;
for (int j = i; j < n; ++j) {
int k = s.charAt(j) - 'a';
freq.merge(cnt[k], -1, Integer::sum);
++cnt[k];
freq.merge(cnt[k], 1, Integer::sum);

if (cnt[k] == 1) {
mi = 1;
}
if (freq.getOrDefault(mi, 0) == 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
}
}
```

### **C++**

```cpp
Expand Down Expand Up @@ -123,6 +179,39 @@ public:
};
```

```cpp
class Solution {
public:
int beautySum(string s) {
int n = s.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
int cnt[26]{};
unordered_map<int, int> freq;
int mi = 1, mx = 1;
for (int j = i; j < n; ++j) {
int k = s[j] - 'a';
--freq[cnt[k]];
++cnt[k];
++freq[cnt[k]];

if (cnt[k] == 1) {
mi = 1;
}
if (freq[mi] == 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
}
};
```

### **Go**

```go
Expand All @@ -149,6 +238,35 @@ func beautySum(s string) (ans int) {
}
```

```go
func beautySum(s string) (ans int) {
n := len(s)
for i := 0; i < n; i++ {
cnt := [26]int{}
freq := map[int]int{}
mi, mx := 1, 1
for j := i; j < n; j++ {
k := int(s[j] - 'a')
freq[cnt[k]]--
cnt[k]++
freq[cnt[k]]++

if cnt[k] == 1 {
mi = 1
}
if freq[mi] == 0 {
mi++
}
if cnt[k] > mx {
mx = cnt[k]
}
ans += mx - mi
}
}
return
}
```

### **JavaScript**

```js
Expand All @@ -170,6 +288,39 @@ var beautySum = function (s) {
};
```

```js
/**
* @param {string} s
* @return {number}
*/
var beautySum = function (s) {
const n = s.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
const cnt = Array(26).fill(0);
const freq = new Map();
let [mi, mx] = [1, 1];
for (let j = i; j < n; ++j) {
const k = s[j].charCodeAt() - 97;
freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1);
++cnt[k];
freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1);
if (cnt[k] === 1) {
mi = 1;
}
if (freq.get(mi) === 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
};
```

### **...**

```
Expand Down
157 changes: 157 additions & 0 deletions solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@

## Solutions

**Solution 1: Enumeration + Counting**

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.

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$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -53,6 +59,30 @@ class Solution:
return ans
```

```python
class Solution:
def beautySum(self, s: str) -> int:
ans, n = 0, len(s)
for i in range(n):
cnt = Counter()
freq = Counter()
mi = mx = 1
for j in range(i, n):
freq[cnt[s[j]]] -= 1
cnt[s[j]] += 1
freq[cnt[s[j]]] += 1

if cnt[s[j]] == 1:
mi = 1
if freq[mi] == 0:
mi += 1
if cnt[s[j]] > mx:
mx = cnt[s[j]]

ans += mx - mi
return ans
```

### **Java**

```java
Expand All @@ -79,6 +109,38 @@ class Solution {
}
```

```java
class Solution {
public int beautySum(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; ++i) {
int[] cnt = new int[26];
Map<Integer, Integer> freq = new HashMap<>();
int mi = 1, mx = 1;
for (int j = i; j < n; ++j) {
int k = s.charAt(j) - 'a';
freq.merge(cnt[k], -1, Integer::sum);
++cnt[k];
freq.merge(cnt[k], 1, Integer::sum);

if (cnt[k] == 1) {
mi = 1;
}
if (freq.getOrDefault(mi, 0) == 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
}
}
```

### **C++**

```cpp
Expand Down Expand Up @@ -107,6 +169,39 @@ public:
};
```

```cpp
class Solution {
public:
int beautySum(string s) {
int n = s.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
int cnt[26]{};
unordered_map<int, int> freq;
int mi = 1, mx = 1;
for (int j = i; j < n; ++j) {
int k = s[j] - 'a';
--freq[cnt[k]];
++cnt[k];
++freq[cnt[k]];

if (cnt[k] == 1) {
mi = 1;
}
if (freq[mi] == 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
}
};
```

### **Go**

```go
Expand All @@ -133,6 +228,35 @@ func beautySum(s string) (ans int) {
}
```

```go
func beautySum(s string) (ans int) {
n := len(s)
for i := 0; i < n; i++ {
cnt := [26]int{}
freq := map[int]int{}
mi, mx := 1, 1
for j := i; j < n; j++ {
k := int(s[j] - 'a')
freq[cnt[k]]--
cnt[k]++
freq[cnt[k]]++

if cnt[k] == 1 {
mi = 1
}
if freq[mi] == 0 {
mi++
}
if cnt[k] > mx {
mx = cnt[k]
}
ans += mx - mi
}
}
return
}
```

### **JavaScript**

```js
Expand All @@ -154,6 +278,39 @@ var beautySum = function (s) {
};
```

```js
/**
* @param {string} s
* @return {number}
*/
var beautySum = function (s) {
const n = s.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
const cnt = Array(26).fill(0);
const freq = new Map();
let [mi, mx] = [1, 1];
for (let j = i; j < n; ++j) {
const k = s[j].charCodeAt() - 97;
freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1);
++cnt[k];
freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1);
if (cnt[k] === 1) {
mi = 1;
}
if (freq.get(mi) === 0) {
++mi;
}
if (cnt[k] > mx) {
mx = cnt[k];
}
ans += mx - mi;
}
}
return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

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

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

<!-- tabs:start -->

Expand Down
Loading