Replace the Substring for Balanced String
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Sliding Window.
Replace the Substring for Balanced String
Find the minimum length of a substring that can be replaced to make all characters ('Q', 'W', 'E', 'R') appear exactly
n/4 times.Visual Representation
s = "QQWE", n = 4, target = 1
Counts: Q:2, W:1, E:1, R:0
Over-limit: Q (2 > 1)
Goal: Find smallest window that contains the 'excess' characters.
Window [Q] (at index 0):
Remaining Counts outside window: Q:1, W:1, E:1, R:0
All remaining counts <= target? YES! (1, 1, 1, 0 <= 1)
Min Length = 1Examples
Input: s = "QQWE"
Output: 1
Approach 1
Level I: Brute Force
Intuition
Iterate through every possible substring that could be replaced. For each substring, calculate the character frequencies of the remaining part of the string. If all counts (Q, W, E, R) in the remaining part are , then the substring can be replaced to make the string balanced. Find the minimum length of such a substring.
Thought Process
- Outer loop
ifrom0ton-1. - Inner loop
jfromiton-1. - Count characters in
s[0...i-1]ands[j+1...n-1]. - If all counts ,
minLen = min(minLen, j - i + 1). - Handle the special case where the string is already balanced (length 0).
⏱ O(N^3) (can be O(N^2) if count updated incrementally)💾 O(1)
Detailed Dry Run
| Substring [i, j] | Remaining Counts | Valid? |
|---|---|---|
| Global | Q:2, W:1, E:1, R:0 | NO |
| [0, 0] (Q) | Q:1, W:1, E:1, R:0 | YES! (Len 1) |
| [1, 1] (Q) | Q:1, W:1, E:1, R:0 | YES! (Len 1) |
Approach 2
Level II: Binary Search on Window Size (O(N log N))
Intuition
We can binary search for the minimum length
L of the substring to replace. For a fixed length L, we check if any substring of size L can be replaced to make the entire string balanced. A string is balanced if all counts of 'Q', 'W', 'E', 'R' are exactly . Replacing a substring means the counts of remaining characters must be .Thought Process
- Target count for each character is
n/4. - In binary search, for a fixed
midlength:- Slide a window of size
midacross the string. - Count character frequencies outside the window.
- If all counts outside , then length
midis possible.
- Slide a window of size
- Adjust the search range for the minimum possible
mid.
Pattern: Search on Answer Space
⏱ O(N log N)💾 O(1) - alphabet size (4)
Approach 3
Level III: Optimal (Sliding Window)
Intuition
Thought Process
A string is balanced if all counts are . To make a string balanced by replacing a substring, the characters outside that substring must all have counts . We search for the smallest window such that all characters remaining outside it satisfy this condition.
Patterns
- Window Complement: Check characters NOT in the window.
- Smallest Valid Window: Shrink from left while condition is met.
⏱ O(N)💾 O(1)
Detailed Dry Run
| Window | Count Q | Count W | Count E | Count R | Valid? |
|---|---|---|---|---|---|
| Global | 2 | 1 | 1 | 0 | NO |
| [Q] | 1 | 1 | 1 | 0 | YES |
| [QQ] | 0 | 1 | 1 | 0 | YES |
⚠️ Common Pitfalls & Tips
The goal is finding the smallest window that contains ALL excesses. If the string is already balanced, the answer is 0.
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.