Post

4056 Longest Balanced Substring Ii

4056 Longest Balanced Substring Ii

Longest Balanced Substring II image

You are given a string s consisting only of the characters ‘a’, ‘b’, and ‘c’.

A substring of s is called balanced if all distinct characters in the substring appear the same number of times.

Return the length of the longest balanced substring of s.

 

Example 1:

Input: s = “abbac”

Output: 4

Explanation:

The longest balanced substring is “abba” because both distinct characters ‘a’ and ‘b’ each appear exactly 2 times.

Example 2:

Input: s = “aabcc”

Output: 3

Explanation:

The longest balanced substring is “abc” because all distinct characters ‘a’, ‘b’ and ‘c’ each appear exactly 1 time.

Example 3:

Input: s = “aba”

Output: 2

Explanation:

One of the longest balanced substrings is “ab” because both distinct characters ‘a’ and ‘b’ each appear exactly 1 time. Another longest balanced substring is “ba”.

 

Constraints:

1
2
1 <= s.length <= 105
s contains only the characters 'a', 'b', and 'c'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class Solution:
    def longestBalanced(self, S: str) -> int:
        N = len(S)
        P = [[0, 0, 0]]
        for c in S:
            P.append(P[-1][:])
            P[-1]["abc".index(c)] += 1

        ans = 0
        first = {}
        for i, (a, b, c) in enumerate(P):
            for key in [
                ("abc", a - b, a - c),
                ("ab", a - b, c),
                ("bc", b - c, a),
                ("ca", c - a, b),
                ("a", b, c),
                ("b", c, a),
                ("c", a, b),
            ]:
                ans = max(ans, i - first.get(key, i))
                first.setdefault(key, i)

        return ans



This post is licensed under CC BY 4.0 by the author.