const getMaxStringLength = (s, k) => {
const map = new Map()
let left = 0;
let right = 0;
let result = 0;
 
while(right < s.length) {
map.set(s[right], (map.get(s[right]) ?? 0) + 1)
while(map.size > k) {
const charCount = map.get(s[left])
if(charCount > 1) {
map.set(s[left], charCount - 1)
} else {
map.delete(s[left])
}
left++
}
result = Math.max(result, right - left + 1)
right++
}
return result
}
 
console.log(getMaxStringLength("aabacbebebe", 3))
Click Run to execute JavaScript.
Code runs in an isolated environment.