From 5f13c98d231c8696ded41a2ab28712bb73ee0ee3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 12 Oct 2025 14:06:24 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3713 No.3713.Longest Balanced Substring I --- .../README.md | 112 +++++++++++++++++- .../README_EN.md | 112 +++++++++++++++++- .../Solution.cpp | 23 ++++ .../Solution.go | 20 ++++ .../Solution.java | 22 ++++ .../Solution.py | 15 +++ .../Solution.ts | 19 +++ 7 files changed, 315 insertions(+), 8 deletions(-) create mode 100644 solution/3700-3799/3713.Longest Balanced Substring I/Solution.cpp create mode 100644 solution/3700-3799/3713.Longest Balanced Substring I/Solution.go create mode 100644 solution/3700-3799/3713.Longest Balanced Substring I/Solution.java create mode 100644 solution/3700-3799/3713.Longest Balanced Substring I/Solution.py create mode 100644 solution/3700-3799/3713.Longest Balanced Substring I/Solution.ts diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/README.md b/solution/3700-3799/3713.Longest Balanced Substring I/README.md index abdb2ceb1a7ce..681fbf10a5508 100644 --- a/solution/3700-3799/3713.Longest Balanced Substring I/README.md +++ b/solution/3700-3799/3713.Longest Balanced Substring I/README.md @@ -76,32 +76,136 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3713.Lo -### 方法一 +### 方法一:枚举 + 计数 + +我们可以在 $[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)$。 + +时间复杂度 $O(n^2)$,其中 $n$ 是字符串的长度。空间复杂度 $O(|\Sigma|)$,其中 $|\Sigma|$ 是字符集的大小,本题中 $|\Sigma| = 26$。 #### Python3 ```python - +class Solution: + def longestBalanced(self, s: str) -> int: + n = len(s) + ans = 0 + for i in range(n): + cnt = Counter() + mx = v = 0 + for j in range(i, n): + cnt[s[j]] += 1 + mx = max(mx, cnt[s[j]]) + if cnt[s[j]] == 1: + v += 1 + if mx * v == j - i + 1: + ans = max(ans, j - i + 1) + return ans ``` #### Java ```java - +class Solution { + public int longestBalanced(String s) { + int n = s.length(); + int[] cnt = new int[26]; + int ans = 0; + for (int i = 0; i < n; ++i) { + Arrays.fill(cnt, 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s.charAt(j) - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(string s) { + int n = s.size(); + vector cnt(26, 0); + int ans = 0; + for (int i = 0; i < n; ++i) { + fill(cnt.begin(), cnt.end(), 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s[j] - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; ``` #### Go ```go +func longestBalanced(s string) (ans int) { + n := len(s) + for i := 0; i < n; i++ { + cnt := [26]int{} + mx, v := 0, 0 + for j := i; j < n; j++ { + c := s[j] - 'a' + cnt[c]++ + if cnt[c] == 1 { + v++ + } + mx = max(mx, cnt[c]) + if mx*v == j-i+1 { + ans = max(ans, j-i+1) + } + } + } + + return ans +} +``` +#### TypeScript + +```ts +function longestBalanced(s: string): number { + const n = s.length; + let ans: number = 0; + for (let i = 0; i < n; ++i) { + const cnt: number[] = Array(26).fill(0); + let [mx, v] = [0, 0]; + for (let j = i; j < n; ++j) { + const c = s[j].charCodeAt(0) - 97; + if (++cnt[c] === 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v === j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md b/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md index fc24a941f15ef..9ae8d577e851f 100644 --- a/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md +++ b/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md @@ -72,32 +72,136 @@ A substring is a contiguous non-empty sequence of charac -### Solution 1 +### Solution 1: Enumeration + Counting + +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)$. + +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. #### Python3 ```python - +class Solution: + def longestBalanced(self, s: str) -> int: + n = len(s) + ans = 0 + for i in range(n): + cnt = Counter() + mx = v = 0 + for j in range(i, n): + cnt[s[j]] += 1 + mx = max(mx, cnt[s[j]]) + if cnt[s[j]] == 1: + v += 1 + if mx * v == j - i + 1: + ans = max(ans, j - i + 1) + return ans ``` #### Java ```java - +class Solution { + public int longestBalanced(String s) { + int n = s.length(); + int[] cnt = new int[26]; + int ans = 0; + for (int i = 0; i < n; ++i) { + Arrays.fill(cnt, 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s.charAt(j) - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(string s) { + int n = s.size(); + vector cnt(26, 0); + int ans = 0; + for (int i = 0; i < n; ++i) { + fill(cnt.begin(), cnt.end(), 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s[j] - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; ``` #### Go ```go +func longestBalanced(s string) (ans int) { + n := len(s) + for i := 0; i < n; i++ { + cnt := [26]int{} + mx, v := 0, 0 + for j := i; j < n; j++ { + c := s[j] - 'a' + cnt[c]++ + if cnt[c] == 1 { + v++ + } + mx = max(mx, cnt[c]) + if mx*v == j-i+1 { + ans = max(ans, j-i+1) + } + } + } + + return ans +} +``` +#### TypeScript + +```ts +function longestBalanced(s: string): number { + const n = s.length; + let ans: number = 0; + for (let i = 0; i < n; ++i) { + const cnt: number[] = Array(26).fill(0); + let [mx, v] = [0, 0]; + for (let j = i; j < n; ++j) { + const c = s[j].charCodeAt(0) - 97; + if (++cnt[c] === 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v === j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.cpp b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.cpp new file mode 100644 index 0000000000000..2dc50e7122a7f --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int longestBalanced(string s) { + int n = s.size(); + vector cnt(26, 0); + int ans = 0; + for (int i = 0; i < n; ++i) { + fill(cnt.begin(), cnt.end(), 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s[j] - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.go b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.go new file mode 100644 index 0000000000000..976532eba4e38 --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.go @@ -0,0 +1,20 @@ +func longestBalanced(s string) (ans int) { + n := len(s) + for i := 0; i < n; i++ { + cnt := [26]int{} + mx, v := 0, 0 + for j := i; j < n; j++ { + c := s[j] - 'a' + cnt[c]++ + if cnt[c] == 1 { + v++ + } + mx = max(mx, cnt[c]) + if mx*v == j-i+1 { + ans = max(ans, j-i+1) + } + } + } + + return ans +} diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.java b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.java new file mode 100644 index 0000000000000..113ad9a52f07c --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int longestBalanced(String s) { + int n = s.length(); + int[] cnt = new int[26]; + int ans = 0; + for (int i = 0; i < n; ++i) { + Arrays.fill(cnt, 0); + int mx = 0, v = 0; + for (int j = i; j < n; ++j) { + int c = s.charAt(j) - 'a'; + if (++cnt[c] == 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v == j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.py b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.py new file mode 100644 index 0000000000000..c1eb22267365d --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def longestBalanced(self, s: str) -> int: + n = len(s) + ans = 0 + for i in range(n): + cnt = Counter() + mx = v = 0 + for j in range(i, n): + cnt[s[j]] += 1 + mx = max(mx, cnt[s[j]]) + if cnt[s[j]] == 1: + v += 1 + if mx * v == j - i + 1: + ans = max(ans, j - i + 1) + return ans diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.ts b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.ts new file mode 100644 index 0000000000000..a7c60ed0acb1f --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.ts @@ -0,0 +1,19 @@ +function longestBalanced(s: string): number { + const n = s.length; + let ans: number = 0; + for (let i = 0; i < n; ++i) { + const cnt: number[] = Array(26).fill(0); + let [mx, v] = [0, 0]; + for (let j = i; j < n; ++j) { + const c = s[j].charCodeAt(0) - 97; + if (++cnt[c] === 1) { + ++v; + } + mx = Math.max(mx, cnt[c]); + if (mx * v === j - i + 1) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +}