I can see the solution works but I’m a bit confused why in the first part of the loop we only ever increase matched when its very possible that adding a new character to the frequency will take a previously matched character from 0 to - 1 which should decrement matched by 1.
const rightChar = str[windowEnd];
if (rightChar in charFrequency) {
// Decrement the frequency of matched character
charFrequency[rightChar] -= 1;
if (charFrequency[rightChar] === 0) {
matched += 1;
}
}
I would think we would need to handle both creating a new match and un-matching a character but when I modify the code to take that into account I start getting failing tests. My brain just can’t wrap itself around why this solution works when the matched count is sometimes out of sync with the character counts. Hope someone can help me understand why this work.
Thank you