educative.io

Educative

Why do we let the character count go into negative

Why do we let the character count go into negative? and why it doesnt matter for the solution?

e.g. for StringPermutation::findPermutation(“aaaadacb”, “abc”)
charFrequencyMap[‘a’] will go negative and we never correct that becuase check is always on if 0:
if (charFrequencyMap[leftChar] == 0) {

        matched--;  // before putting the character back, decrement the matched count

      }

The charFrequencyMap[leftChar] is incremented so it will not become negative at any stage.

right char does.
charFrequencyMap[rightChar]–;