-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1234-replace-the-substring-for-balanced-string.js
More file actions
44 lines (37 loc) · 1.16 KB
/
1234-replace-the-substring-for-balanced-string.js
File metadata and controls
44 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 1234. Replace the Substring for Balanced String
* https://leetcode.com/problems/replace-the-substring-for-balanced-string/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {string} s
* @return {number}
*/
var balancedString = function(s) {
const target = s.length / 4;
const charCount = { Q: 0, W: 0, E: 0, R: 0 };
for (const char of s) {
charCount[char]++;
}
if (Object.values(charCount).every(count => count === target)) return 0;
let result = s.length;
let left = 0;
for (let right = 0; right < s.length; right++) {
charCount[s[right]]--;
while (left <= right && Object.values(charCount).every(count => count <= target)) {
result = Math.min(result, right - left + 1);
charCount[s[left]]++;
left++;
}
}
return result;
};