@@ -89,25 +89,382 @@ tags:
8989#### Python3
9090
9191``` python
92-
92+ class Solution :
93+ def longestBalanced (self , s : str ) -> int :
94+ def calc1 (s : str ) -> int :
95+ res = 0
96+ i, n = 0 , len (s)
97+ while i < n:
98+ j = i + 1
99+ while j < n and s[j] == s[i]:
100+ j += 1
101+ res = max (res, j - i)
102+ i = j
103+ return res
104+
105+ def calc2 (s : str , a : str , b : str ) -> int :
106+ res = 0
107+ i, n = 0 , len (s)
108+ while i < n:
109+ while i < n and s[i] not in (a, b):
110+ i += 1
111+ pos = {0 : i - 1 }
112+ d = 0
113+ while i < n and s[i] in (a, b):
114+ d += 1 if s[i] == a else - 1
115+ if d in pos:
116+ res = max (res, i - pos[d])
117+ else :
118+ pos[d] = i
119+ i += 1
120+ return res
121+
122+ def calc3 (s : str ) -> int :
123+ pos = {(0 , 0 ): - 1 }
124+ cnt = Counter()
125+ res = 0
126+ for i, c in enumerate (s):
127+ cnt[c] += 1
128+ k = (cnt[" a" ] - cnt[" b" ], cnt[" b" ] - cnt[" c" ])
129+ if k in pos:
130+ res = max (res, i - pos[k])
131+ else :
132+ pos[k] = i
133+ return res
134+
135+ x = calc1(s)
136+ y = max (calc2(s, " a" , " b" ), calc2(s, " b" , " c" ), calc2(s, " a" , " c" ))
137+ z = calc3(s)
138+ return max (x, y, z)
93139```
94140
95141#### Java
96142
97143``` java
98-
144+ class Solution {
145+ public int longestBalanced (String s ) {
146+ char [] cs = s. toCharArray();
147+ int x = calc1(cs);
148+ int y = Math . max(calc2(cs, ' a' , ' b' ), Math . max(calc2(cs, ' b' , ' c' ), calc2(cs, ' a' , ' c' )));
149+ int z = calc3(cs);
150+ return Math . max(x, Math . max(y, z));
151+ }
152+
153+ private int calc1 (char [] s ) {
154+ int res = 0 ;
155+ int i = 0 , n = s. length;
156+ while (i < n) {
157+ int j = i + 1 ;
158+ while (j < n && s[j] == s[i]) {
159+ j++ ;
160+ }
161+ res = Math . max(res, j - i);
162+ i = j;
163+ }
164+ return res;
165+ }
166+
167+ private int calc2 (char [] s , char a , char b ) {
168+ int res = 0 ;
169+ int i = 0 , n = s. length;
170+ while (i < n) {
171+ while (i < n && s[i] != a && s[i] != b) {
172+ i++ ;
173+ }
174+ Map<Integer , Integer > pos = new HashMap<> ();
175+ pos. put(0 , i - 1 );
176+ int d = 0 ;
177+ while (i < n && (s[i] == a || s[i] == b)) {
178+ d += (s[i] == a) ? 1 : - 1 ;
179+ Integer prev = pos. get(d);
180+ if (prev != null ) {
181+ res = Math . max(res, i - prev);
182+ } else {
183+ pos. put(d, i);
184+ }
185+ i++ ;
186+ }
187+ }
188+ return res;
189+ }
190+
191+ private int calc3 (char [] s ) {
192+ Map<Long , Integer > pos = new HashMap<> ();
193+ pos. put(f(0 , 0 ), - 1 );
194+
195+ int [] cnt = new int [3 ];
196+ int res = 0 ;
197+
198+ for (int i = 0 ; i < s. length; i++ ) {
199+ char c = s[i];
200+ ++ cnt[c - ' a' ];
201+ int x = cnt[0 ] - cnt[1 ];
202+ int y = cnt[1 ] - cnt[2 ];
203+ long k = f(x, y);
204+
205+ Integer prev = pos. get(k);
206+ if (prev != null ) {
207+ res = Math . max(res, i - prev);
208+ } else {
209+ pos. put(k, i);
210+ }
211+ }
212+ return res;
213+ }
214+
215+ private long f (int x , int y ) {
216+ return (x + 100000 ) << 20 | (y + 100000 );
217+ }
218+ }
99219```
100220
101221#### C++
102222
103223``` cpp
104-
224+ class Solution {
225+ public:
226+ int longestBalanced(string s) {
227+ int x = calc1(s);
228+ int y = max({calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')});
229+ int z = calc3(s);
230+ return max({x, y, z});
231+ }
232+
233+ private:
234+ int calc1(const string& s) {
235+ int res = 0;
236+ int i = 0, n = s.size();
237+ while (i < n) {
238+ int j = i + 1;
239+ while (j < n && s[ j] == s[ i] ) {
240+ ++j;
241+ }
242+ res = max(res, j - i);
243+ i = j;
244+ }
245+ return res;
246+ }
247+
248+ int calc2(const string& s, char a, char b) {
249+ int res = 0;
250+ int i = 0, n = s.size();
251+ while (i < n) {
252+ while (i < n && s[i] != a && s[i] != b) {
253+ ++i;
254+ }
255+
256+ unordered_map<int , int > pos;
257+ pos[0 ] = i - 1 ;
258+
259+ int d = 0 ;
260+ while (i < n && (s[i] == a || s[i] == b)) {
261+ d += (s[i] == a) ? 1 : -1;
262+ auto it = pos.find(d);
263+ if (it != pos.end()) {
264+ res = max(res, i - it->second);
265+ } else {
266+ pos[d] = i;
267+ }
268+ i++;
269+ }
270+ }
271+ return res;
272+ }
273+
274+ static long long f(int x, int y) {
275+ return ((long long) (x + 100000) << 20) | (long long) (y + 100000);
276+ }
277+
278+ int calc3(const string& s) {
279+ unordered_map<long long, int> pos;
280+ pos[f(0, 0)] = -1;
281+
282+ int cnt[3] = {0, 0, 0};
283+ int res = 0;
284+
285+ for (int i = 0; i < (int) s.size(); i++) {
286+ char c = s[i];
287+ ++cnt[c - 'a'];
288+ int x = cnt[0] - cnt[1];
289+ int y = cnt[1] - cnt[2];
290+ long long k = f(x, y);
291+
292+ auto it = pos.find(k);
293+ if (it != pos.end()) {
294+ res = max(res, i - it->second);
295+ } else {
296+ pos[k] = i;
297+ }
298+ }
299+ return res;
300+ }
301+ };
105302```
106303
107304#### Go
108305
109306``` go
307+ func longestBalanced (s string ) int {
308+ x := calc1 (s)
309+ y := max (calc2 (s, ' a' , ' b' ), calc2 (s, ' b' , ' c' ), calc2 (s, ' a' , ' c' ))
310+ z := calc3 (s)
311+ return max (x, max (y, z))
312+ }
313+
314+ func calc1 (s string ) int {
315+ res := 0
316+ n := len (s)
317+ i := 0
318+ for i < n {
319+ j := i + 1
320+ for j < n && s[j] == s[i] {
321+ j++
322+ }
323+ if j-i > res {
324+ res = j - i
325+ }
326+ i = j
327+ }
328+ return res
329+ }
330+
331+ func calc2 (s string , a , b byte ) int {
332+ res := 0
333+ n := len (s)
334+ i := 0
335+ for i < n {
336+ for i < n && s[i] != a && s[i] != b {
337+ i++
338+ }
339+ pos := map [int ]int {0 : i - 1 }
340+ d := 0
341+ for i < n && (s[i] == a || s[i] == b) {
342+ if s[i] == a {
343+ d++
344+ } else {
345+ d--
346+ }
347+ if prev , ok := pos[d]; ok {
348+ if i-prev > res {
349+ res = i - prev
350+ }
351+ } else {
352+ pos[d] = i
353+ }
354+ i++
355+ }
356+ }
357+ return res
358+ }
359+
360+ type key struct {
361+ x, y int
362+ }
363+
364+ func calc3 (s string ) int {
365+ pos := make (map [key]int )
366+ pos[key{0 , 0 }] = -1
367+
368+ cnt := [3 ]int {}
369+ res := 0
370+
371+ for i := 0 ; i < len (s); i++ {
372+ c := s[i]
373+ cnt[c-' a' ]++
374+ x := cnt[0 ] - cnt[1 ]
375+ y := cnt[1 ] - cnt[2 ]
376+ k := key{x, y}
377+
378+ if j , ok := pos[k]; ok {
379+ if i-j > res {
380+ res = i - j
381+ }
382+ } else {
383+ pos[k] = i
384+ }
385+ }
386+ return res
387+ }
388+ ```
110389
390+ #### TypeScript
391+
392+ ``` ts
393+ function longestBalanced(s : string ): number {
394+ const x = calc1 (s );
395+ const y = Math .max (calc2 (s , ' a' , ' b' ), calc2 (s , ' b' , ' c' ), calc2 (s , ' a' , ' c' ));
396+ const z = calc3 (s );
397+ return Math .max (x , y , z );
398+ }
399+
400+ function calc1(s : string ): number {
401+ let res = 0 ;
402+ const n = s .length ;
403+ let i = 0 ;
404+ while (i < n ) {
405+ let j = i + 1 ;
406+ while (j < n && s [j ] === s [i ]) j ++ ;
407+ res = Math .max (res , j - i );
408+ i = j ;
409+ }
410+ return res ;
411+ }
412+
413+ function calc2(s : string , a : string , b : string ): number {
414+ let res = 0 ;
415+ const n = s .length ;
416+ let i = 0 ;
417+
418+ while (i < n ) {
419+ while (i < n && s [i ] !== a && s [i ] !== b ) i ++ ;
420+
421+ const pos = new Map <number , number >();
422+ pos .set (0 , i - 1 );
423+
424+ let d = 0 ;
425+ while (i < n && (s [i ] === a || s [i ] === b )) {
426+ d += s [i ] === a ? 1 : - 1 ;
427+
428+ const prev = pos .get (d );
429+ if (prev !== undefined ) {
430+ res = Math .max (res , i - prev );
431+ } else {
432+ pos .set (d , i );
433+ }
434+ i ++ ;
435+ }
436+ }
437+ return res ;
438+ }
439+
440+ function calc3(s : string ): number {
441+ const pos = new Map <string , number >();
442+ pos .set (key (0 , 0 ), - 1 );
443+
444+ const cnt = [0 , 0 , 0 ];
445+ let res = 0 ;
446+
447+ for (let i = 0 ; i < s .length ; i ++ ) {
448+ const c = s .charCodeAt (i ) - 97 ;
449+ cnt [c ]++ ;
450+
451+ const x = cnt [0 ] - cnt [1 ];
452+ const y = cnt [1 ] - cnt [2 ];
453+ const k = key (x , y );
454+
455+ const prev = pos .get (k );
456+ if (prev !== undefined ) {
457+ res = Math .max (res , i - prev );
458+ } else {
459+ pos .set (k , i );
460+ }
461+ }
462+ return res ;
463+ }
464+
465+ function key(x : number , y : number ): string {
466+ return x + ' #' + y ;
467+ }
111468```
112469
113470<!-- tabs:end -->
0 commit comments