3714. Longest Balanced Substring II Medium
1/**
2 * [3714] Longest Balanced Substring II
3 *
4 * You are given a string s consisting only of the characters 'a', 'b', and 'c'.
5 * A <span data-keyword="substring-nonempty">substring</span> of s is called balanced if all distinct characters in the substring appear the same number of times.
6 * Return the length of the longest balanced substring of s.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "abbac"</span>
11 * Output: <span class="example-io">4</span>
12 * Explanation:
13 * The longest balanced substring is "abba" because both distinct characters 'a' and 'b' each appear exactly 2 times.
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "aabcc"</span>
18 * Output: <span class="example-io">3</span>
19 * Explanation:
20 * The longest balanced substring is "abc" because all distinct characters 'a', 'b' and 'c' each appear exactly 1 time.
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">s = "aba"</span>
25 * Output: <span class="example-io">2</span>
26 * Explanation:
27 * 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".
28 * </div>
29 *
30 * Constraints:
31 *
32 * 1 <= s.length <= 10^5
33 * s contains only the characters 'a', 'b', and 'c'.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/longest-balanced-substring-ii/
39// discuss: https://leetcode.com/problems/longest-balanced-substring-ii/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn longest_balanced(s: String) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_3714() {
57 }
58}
59Back
© 2026 bowen.ge All Rights Reserved.