Class Solution
java.lang.Object
g1201_1300.s1234_replace_the_substring_for_balanced_string.Solution
1234 - Replace the Substring for Balanced String.<p>Medium</p>
<p>You are given a string s of length <code>n</code> containing only four kinds of characters: <code>'Q'</code>, <code>'W'</code>, <code>'E'</code>, and <code>'R'</code>.</p>
<p>A string is said to be <strong>balanced</strong> if each of its characters appears <code>n / 4</code> times where <code>n</code> is the length of the string.</p>
<p>Return <em>the minimum length of the substring that can be replaced with <strong>any</strong> other string of the same length to make</em> <code>s</code> <em><strong>balanced</strong></em>. If s is already <strong>balanced</strong> , return <code>0</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> s = “QWER”</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> s is already balanced.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> s = “QQWE”</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> We need to replace a ‘Q’ to ‘R’, so that “RQWE” (or “QRWE”) is balanced.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> s = “QQQW”</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> We can replace the first “QQ” to “ER”.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == s.length</code></li>
<li><code>4 <= n <= 10<sup>5</sup></code></li>
<li><code>n</code> is a multiple of <code>4</code>.</li>
<li><code>s</code> contains only <code>'Q'</code>, <code>'W'</code>, <code>'E'</code>, and <code>'R'</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
balancedString
-