Replace the Substring for Balanced String

You are given a string s of length n containing only four kinds of characters: 'Q', 'W', 'E', and 'R'.

A string is said to be balanced if each of its characters appears n / 4 times where n is 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 s balanced. If s is already balanced, return 0.

Example 1
Inputs = "QWER"
Output0
s is already balanced.
Example 2
Inputs = "QQWE"
Output1
We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.

Constraints

  • n == s.length
  • 4 <= n <= 10^5
  • n is a multiple of 4.
  • s contains only 'Q', 'W', 'E', and 'R'.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate