java.lang.Object
g1201_1300.s1234_replace_the_substring_for_balanced_string.Solution

public class Solution extends Object
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 = &ldquo;QWER&rdquo;</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 = &ldquo;QQWE&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> We need to replace a &lsquo;Q&rsquo; to &lsquo;R&rsquo;, so that &ldquo;RQWE&rdquo; (or &ldquo;QRWE&rdquo;) is balanced.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;QQQW&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> We can replace the first &ldquo;QQ&rdquo; to &ldquo;ER&rdquo;.</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 Details

    • Solution

      public Solution()
  • Method Details

    • balancedString

      public int balancedString(String s)