<p>You are given a string s of length <code>n</code> containing only four kinds of characters: <code>&#39;Q&#39;</code>, <code>&#39;W&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;R&#39;</code>.</p>

<p>A string is said to be <strong>balanced</strong><em> </em>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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;QWER&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> s is already balanced.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;QQWE&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> We need to replace a &#39;Q&#39; to &#39;R&#39;, so that &quot;RQWE&quot; (or &quot;QRWE&quot;) is balanced.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;QQQW&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can replace the first &quot;QQ&quot; to &quot;ER&quot;. 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == s.length</code></li>
	<li><code>4 &lt;= n &lt;= 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>&#39;Q&#39;</code>, <code>&#39;W&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;R&#39;</code>.</li>
</ul>

class Solution {
public:
    int balancedString(const std::string &s) {
      static auto getOccurrence = [](int *occur, char ch) -> int & {
          switch (ch) {
            case 'Q':
              return occur[0];
            case 'W':
              return occur[1];
            case 'E':
              return occur[2];
            case 'R':
              return occur[3];
            default:
              __builtin_unreachable();
          }
      };

      static auto greaterOrEqual = [](int *occur1, int *occur2) {
        for (int i = 0; i < 4; ++i) { if (occur1[i] < occur2[i]) return false; }
        return true;
      };

      int n = s.size(), leastChanged = 0, occur[4] = {0};
      for (auto ch : s) ++getOccurrence(occur, ch);
      for (auto &o : occur) {
        if (o > n / 4) {
          o -= n / 4;
          leastChanged += o;
        } else {
          o = 0;
        }
      }

#if 0
      printf("Q:%d, W:%d, E:%d, R:%d\n", occur[0], occur[1], occur[2], occur[3]);
#endif
      if (leastChanged == 0) return 0;

      int occur2[4] = {0}, minSteps = n;
      for (int l = 0, r = 0; l < n; ++r) {
        assert(minSteps >= leastChanged);
        if (minSteps == leastChanged) break;

        if (r < n) {
          if (getOccurrence(occur, s[r])) ++getOccurrence(occur2, s[r]);
        } else {
          if (getOccurrence(occur, s[l])) --getOccurrence(occur2, s[l]);
          ++l;
        }

        while (l < n && getOccurrence(occur, s[l]) == 0) {
          ++l;
        }

#if 0
        printf("Q2:%d, W2:%d, E2:%d, R2:%d, step: %d, l=%d(%c), r=%d(%c)\n", occur2[0],
               occur2[1], occur2[2], occur2[3], minSteps, l, s[l], r, (r < n ? s[r] : '?'));
#endif

        // assert(getOccurrence(occur, s[l]) > 0);
        while (greaterOrEqual(occur2, occur)) {
          minSteps = std::min(std::min(r, n-1)- l + 1, minSteps);
          if (getOccurrence(occur2, s[l])) --getOccurrence(occur2, s[l]);
          l++;
        }
      }
      return minSteps;
    }
};

8900