22. Replace the Substring for Balanced String
You are given a string containing only 4 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 the original string s
balanced.
Return 0 if the string is already balanced.
Example 1:
Example 2:
Example 3:
Example 4:
Solution: (Sliding Window)
Approach: Count number of each occurrences of each character. If any char occurs more then count the number of times it occurs more. Find the minimum windows substring to eliminate this additional count
Time Complexity: O(n)
Previous21. Subarrays with K Different IntegersNext23. Maximize the Confusion of an Exam / Max Consecutive Ones III
Last updated