Skip to content

Commit 5f13c98

Browse files
committed
feat: add solutions to lc problem: No.3713
No.3713.Longest Balanced Substring I
1 parent 781d852 commit 5f13c98

File tree

7 files changed

+315
-8
lines changed

7 files changed

+315
-8
lines changed

solution/3700-3799/3713.Longest Balanced Substring I/README.md

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,136 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3713.Lo
7676

7777
<!-- solution:start -->
7878

79-
### 方法一
79+
### 方法一:枚举 + 计数
80+
81+
我们可以在 $[0,..n-1]$ 范围内枚举子串的起始位置 $i$,然后在 $[i,..,n-1]$ 范围内枚举子串的结束位置 $j$,并使用哈希表 $\textit{cnt}$ 记录子串 $s[i..j]$ 中每个字符出现的次数。我们使用变量 $\textit{mx}$ 记录子串中出现次数最多的字符的出现次数,使用变量 $v$ 记录子串中不同字符的个数。如果在某个位置 $j$,满足 $\textit{mx} \times v = j - i + 1$,则说明子串 $s[i..j]$ 是一个平衡子串,我们更新答案 $\textit{ans} = \max(\textit{ans}, j - i + 1)$。
82+
83+
时间复杂度 $O(n^2)$,其中 $n$ 是字符串的长度。空间复杂度 $O(|\Sigma|)$,其中 $|\Sigma|$ 是字符集的大小,本题中 $|\Sigma| = 26$。
8084

8185
<!-- tabs:start -->
8286

8387
#### Python3
8488

8589
```python
86-
90+
class Solution:
91+
def longestBalanced(self, s: str) -> int:
92+
n = len(s)
93+
ans = 0
94+
for i in range(n):
95+
cnt = Counter()
96+
mx = v = 0
97+
for j in range(i, n):
98+
cnt[s[j]] += 1
99+
mx = max(mx, cnt[s[j]])
100+
if cnt[s[j]] == 1:
101+
v += 1
102+
if mx * v == j - i + 1:
103+
ans = max(ans, j - i + 1)
104+
return ans
87105
```
88106

89107
#### Java
90108

91109
```java
92-
110+
class Solution {
111+
public int longestBalanced(String s) {
112+
int n = s.length();
113+
int[] cnt = new int[26];
114+
int ans = 0;
115+
for (int i = 0; i < n; ++i) {
116+
Arrays.fill(cnt, 0);
117+
int mx = 0, v = 0;
118+
for (int j = i; j < n; ++j) {
119+
int c = s.charAt(j) - 'a';
120+
if (++cnt[c] == 1) {
121+
++v;
122+
}
123+
mx = Math.max(mx, cnt[c]);
124+
if (mx * v == j - i + 1) {
125+
ans = Math.max(ans, j - i + 1);
126+
}
127+
}
128+
}
129+
return ans;
130+
}
131+
}
93132
```
94133

95134
#### C++
96135

97136
```cpp
98-
137+
class Solution {
138+
public:
139+
int longestBalanced(string s) {
140+
int n = s.size();
141+
vector<int> cnt(26, 0);
142+
int ans = 0;
143+
for (int i = 0; i < n; ++i) {
144+
fill(cnt.begin(), cnt.end(), 0);
145+
int mx = 0, v = 0;
146+
for (int j = i; j < n; ++j) {
147+
int c = s[j] - 'a';
148+
if (++cnt[c] == 1) {
149+
++v;
150+
}
151+
mx = max(mx, cnt[c]);
152+
if (mx * v == j - i + 1) {
153+
ans = max(ans, j - i + 1);
154+
}
155+
}
156+
}
157+
return ans;
158+
}
159+
};
99160
```
100161
101162
#### Go
102163
103164
```go
165+
func longestBalanced(s string) (ans int) {
166+
n := len(s)
167+
for i := 0; i < n; i++ {
168+
cnt := [26]int{}
169+
mx, v := 0, 0
170+
for j := i; j < n; j++ {
171+
c := s[j] - 'a'
172+
cnt[c]++
173+
if cnt[c] == 1 {
174+
v++
175+
}
176+
mx = max(mx, cnt[c])
177+
if mx*v == j-i+1 {
178+
ans = max(ans, j-i+1)
179+
}
180+
}
181+
}
182+
183+
return ans
184+
}
185+
```
104186

187+
#### TypeScript
188+
189+
```ts
190+
function longestBalanced(s: string): number {
191+
const n = s.length;
192+
let ans: number = 0;
193+
for (let i = 0; i < n; ++i) {
194+
const cnt: number[] = Array(26).fill(0);
195+
let [mx, v] = [0, 0];
196+
for (let j = i; j < n; ++j) {
197+
const c = s[j].charCodeAt(0) - 97;
198+
if (++cnt[c] === 1) {
199+
++v;
200+
}
201+
mx = Math.max(mx, cnt[c]);
202+
if (mx * v === j - i + 1) {
203+
ans = Math.max(ans, j - i + 1);
204+
}
205+
}
206+
}
207+
return ans;
208+
}
105209
```
106210

107211
<!-- tabs:end -->

solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,136 @@ A <strong>substring</strong> is a contiguous <b>non-empty</b> sequence of charac
7272

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

75-
### Solution 1
75+
### Solution 1: Enumeration + Counting
76+
77+
We can enumerate the starting position $i$ of substrings in the range $[0,..n-1]$, then enumerate the ending position $j$ of substrings in the range $[i,..,n-1]$, and use a hash table $\textit{cnt}$ to record the frequency of each character in substring $s[i..j]$. We use variable $\textit{mx}$ to record the maximum frequency of characters in the substring, and use variable $v$ to record the number of distinct characters in the substring. If at some position $j$, we have $\textit{mx} \times v = j - i + 1$, it means substring $s[i..j]$ is a balanced substring, and we update the answer $\textit{ans} = \max(\textit{ans}, j - i + 1)$.
78+
79+
The time complexity is $O(n^2)$, where $n$ is the length of the string. The space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the character set, which is $|\Sigma| = 26$ in this problem.
7680

7781
<!-- tabs:start -->
7882

7983
#### Python3
8084

8185
```python
82-
86+
class Solution:
87+
def longestBalanced(self, s: str) -> int:
88+
n = len(s)
89+
ans = 0
90+
for i in range(n):
91+
cnt = Counter()
92+
mx = v = 0
93+
for j in range(i, n):
94+
cnt[s[j]] += 1
95+
mx = max(mx, cnt[s[j]])
96+
if cnt[s[j]] == 1:
97+
v += 1
98+
if mx * v == j - i + 1:
99+
ans = max(ans, j - i + 1)
100+
return ans
83101
```
84102

85103
#### Java
86104

87105
```java
88-
106+
class Solution {
107+
public int longestBalanced(String s) {
108+
int n = s.length();
109+
int[] cnt = new int[26];
110+
int ans = 0;
111+
for (int i = 0; i < n; ++i) {
112+
Arrays.fill(cnt, 0);
113+
int mx = 0, v = 0;
114+
for (int j = i; j < n; ++j) {
115+
int c = s.charAt(j) - 'a';
116+
if (++cnt[c] == 1) {
117+
++v;
118+
}
119+
mx = Math.max(mx, cnt[c]);
120+
if (mx * v == j - i + 1) {
121+
ans = Math.max(ans, j - i + 1);
122+
}
123+
}
124+
}
125+
return ans;
126+
}
127+
}
89128
```
90129

91130
#### C++
92131

93132
```cpp
94-
133+
class Solution {
134+
public:
135+
int longestBalanced(string s) {
136+
int n = s.size();
137+
vector<int> cnt(26, 0);
138+
int ans = 0;
139+
for (int i = 0; i < n; ++i) {
140+
fill(cnt.begin(), cnt.end(), 0);
141+
int mx = 0, v = 0;
142+
for (int j = i; j < n; ++j) {
143+
int c = s[j] - 'a';
144+
if (++cnt[c] == 1) {
145+
++v;
146+
}
147+
mx = max(mx, cnt[c]);
148+
if (mx * v == j - i + 1) {
149+
ans = max(ans, j - i + 1);
150+
}
151+
}
152+
}
153+
return ans;
154+
}
155+
};
95156
```
96157
97158
#### Go
98159
99160
```go
161+
func longestBalanced(s string) (ans int) {
162+
n := len(s)
163+
for i := 0; i < n; i++ {
164+
cnt := [26]int{}
165+
mx, v := 0, 0
166+
for j := i; j < n; j++ {
167+
c := s[j] - 'a'
168+
cnt[c]++
169+
if cnt[c] == 1 {
170+
v++
171+
}
172+
mx = max(mx, cnt[c])
173+
if mx*v == j-i+1 {
174+
ans = max(ans, j-i+1)
175+
}
176+
}
177+
}
178+
179+
return ans
180+
}
181+
```
100182

183+
#### TypeScript
184+
185+
```ts
186+
function longestBalanced(s: string): number {
187+
const n = s.length;
188+
let ans: number = 0;
189+
for (let i = 0; i < n; ++i) {
190+
const cnt: number[] = Array(26).fill(0);
191+
let [mx, v] = [0, 0];
192+
for (let j = i; j < n; ++j) {
193+
const c = s[j].charCodeAt(0) - 97;
194+
if (++cnt[c] === 1) {
195+
++v;
196+
}
197+
mx = Math.max(mx, cnt[c]);
198+
if (mx * v === j - i + 1) {
199+
ans = Math.max(ans, j - i + 1);
200+
}
201+
}
202+
}
203+
return ans;
204+
}
101205
```
102206

103207
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int longestBalanced(string s) {
4+
int n = s.size();
5+
vector<int> cnt(26, 0);
6+
int ans = 0;
7+
for (int i = 0; i < n; ++i) {
8+
fill(cnt.begin(), cnt.end(), 0);
9+
int mx = 0, v = 0;
10+
for (int j = i; j < n; ++j) {
11+
int c = s[j] - 'a';
12+
if (++cnt[c] == 1) {
13+
++v;
14+
}
15+
mx = max(mx, cnt[c]);
16+
if (mx * v == j - i + 1) {
17+
ans = max(ans, j - i + 1);
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func longestBalanced(s string) (ans int) {
2+
n := len(s)
3+
for i := 0; i < n; i++ {
4+
cnt := [26]int{}
5+
mx, v := 0, 0
6+
for j := i; j < n; j++ {
7+
c := s[j] - 'a'
8+
cnt[c]++
9+
if cnt[c] == 1 {
10+
v++
11+
}
12+
mx = max(mx, cnt[c])
13+
if mx*v == j-i+1 {
14+
ans = max(ans, j-i+1)
15+
}
16+
}
17+
}
18+
19+
return ans
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int longestBalanced(String s) {
3+
int n = s.length();
4+
int[] cnt = new int[26];
5+
int ans = 0;
6+
for (int i = 0; i < n; ++i) {
7+
Arrays.fill(cnt, 0);
8+
int mx = 0, v = 0;
9+
for (int j = i; j < n; ++j) {
10+
int c = s.charAt(j) - 'a';
11+
if (++cnt[c] == 1) {
12+
++v;
13+
}
14+
mx = Math.max(mx, cnt[c]);
15+
if (mx * v == j - i + 1) {
16+
ans = Math.max(ans, j - i + 1);
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def longestBalanced(self, s: str) -> int:
3+
n = len(s)
4+
ans = 0
5+
for i in range(n):
6+
cnt = Counter()
7+
mx = v = 0
8+
for j in range(i, n):
9+
cnt[s[j]] += 1
10+
mx = max(mx, cnt[s[j]])
11+
if cnt[s[j]] == 1:
12+
v += 1
13+
if mx * v == j - i + 1:
14+
ans = max(ans, j - i + 1)
15+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function longestBalanced(s: string): number {
2+
const n = s.length;
3+
let ans: number = 0;
4+
for (let i = 0; i < n; ++i) {
5+
const cnt: number[] = Array(26).fill(0);
6+
let [mx, v] = [0, 0];
7+
for (let j = i; j < n; ++j) {
8+
const c = s[j].charCodeAt(0) - 97;
9+
if (++cnt[c] === 1) {
10+
++v;
11+
}
12+
mx = Math.max(mx, cnt[c]);
13+
if (mx * v === j - i + 1) {
14+
ans = Math.max(ans, j - i + 1);
15+
}
16+
}
17+
}
18+
return ans;
19+
}

0 commit comments

Comments
 (0)