1234. Replace the Substring for Balanced String Medium
1/**
2 * [1234] Replace the Substring for Balanced String
3 *
4 * You are given a string s of length n containing only four kinds of characters: 'Q', 'W', 'E', and 'R'.
5 * A string is said to be balanced if each of its characters appears n / 4 times where n is the length of the string.
6 * Return the minimum length of the substring that can be replaced with any other string of the same length to make s balanced. If s is already balanced, return 0.
7 *
8 * Example 1:
9 *
10 * Input: s = "QWER"
11 * Output: 0
12 * Explanation: s is already balanced.
13 *
14 * Example 2:
15 *
16 * Input: s = "QQWE"
17 * Output: 1
18 * Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.
19 *
20 * Example 3:
21 *
22 * Input: s = "QQQW"
23 * Output: 2
24 * Explanation: We can replace the first "QQ" to "ER".
25 *
26 *
27 * Constraints:
28 *
29 * n == s.length
30 * 4 <= n <= 10^5
31 * n is a multiple of 4.
32 * s contains only 'Q', 'W', 'E', and 'R'.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/replace-the-substring-for-balanced-string/
38// discuss: https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn balanced_string(s: String) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_1234() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.