Replace the Substring for Balanced String - Problem

You are given a string s containing only four specific characters: 'Q', 'W', 'E', and 'R'. Think of this as a special encoding where each character must appear in perfect balance.

A string is considered balanced if each of the four characters appears exactly n/4 times, where n is the total length of the string. Your goal is to find the minimum length substring that you can replace with any other string of the same length to make the entire string balanced.

Example: If s = "QWER", it's already balanced (each character appears once). If s = "QQWE", you need to replace at least one character to balance it.

Return 0 if the string is already balanced, otherwise return the minimum replacement length needed.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "QWER"
โ€บ Output: 0
๐Ÿ’ก Note: The string is already balanced. Each character appears exactly once (n/4 = 4/4 = 1), so no replacement is needed.
example_2.py โ€” Need Replacement
$ Input: s = "QQWE"
โ€บ Output: 1
๐Ÿ’ก Note: We have 2 Q's, 1 W, 1 E, and 0 R's. We need exactly 1 of each. We can replace any single Q with R to get "QRWE" which is balanced.
example_3.py โ€” Multiple Excess
$ Input: s = "QQQQQWWWWWEEEERRRR"
โ€บ Output: 4
๐Ÿ’ก Note: String length is 16, so we need 4 of each character. We have 5 Q's, 5 W's, 4 E's, 4 R's. We need to replace a substring containing the excess Q and W, minimum length is 4.

Visualization

Tap to expand
๐Ÿณ Recipe Balance ProblemNeed exactly equal amounts of Quinoa, Walnuts, Eggplant, RiceCurrent Recipe:Q: 6 (excess: 2)W: 5 (excess: 1)E: 4 (perfect)R: 1 (need 3)Solution - Find Minimum Scoop:Sliding WindowContains 2Q + 1W(All excess ingredients!)โ†’Replace with1Q + 3R(Perfect balance!)๐ŸŽฏ Key Insight: Only replace the portion containing excess ingredientsSliding window finds the minimum substring with all excess charactersTime: O(n), Space: O(1) - optimal solution!
Understanding the Visualization
1
Measure Current Ingredients
Count how much of each ingredient (Q, W, E, R) you currently have
2
Identify Excess
Determine which ingredients exceed the target amount (n/4 of each)
3
Find Optimal Scoop
Use sliding window to find the smallest portion that contains enough excess ingredients
4
Replace for Balance
Replace that portion with the right mix to achieve perfect balance
Key Takeaway
๐ŸŽฏ Key Insight: We only need to find and replace the minimum substring containing all excess characters. The sliding window technique efficiently finds this optimal substring in linear time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string with each character visited at most twice (once by right pointer, once by left pointer)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for character counters and pointers

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • n is divisible by 4
  • s consists only of 'Q', 'W', 'E', and 'R'
  • Note: Since n must be divisible by 4, n/4 is always an integer
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen