Class Solution
-
- All Implemented Interfaces:
public final class Solution1234 - Replace the Substring for Balanced String\.
Medium
You are given a string s of length
ncontaining only four kinds of characters:'Q','W','E', and'R'.A string is said to be balanced if each of its characters appears
n / 4times wherenis the length of the string.Return the minimum length of the substring that can be replaced with any other string of the same length to make
sbalanced. If s is already balanced , return0.Example 1:
Input: s = "QWER"
Output: 0
Explanation: s is already balanced.
Example 2:
Input: s = "QQWE"
Output: 1
Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.
Example 3:
Input: s = "QQQW"
Output: 2
Explanation: We can replace the first "QQ" to "ER".
Constraints:
n == s.length<code>4 <= n <= 10<sup>5</sup></code>
nis a multiple of4.scontains only'Q','W','E', and'R'.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerbalancedString(String s)-
-
Method Detail
-
balancedString
final Integer balancedString(String s)
-
-
-
-