educative.io

No-repeat Substring: incorrect explanation

Lines 12-14 of the solution contain the following info:

  // this is tricky; in the current window, we will not have any 'rightChar' after its previous index
  // and if 'windowStart' is already ahead of the last index of 'rightChar', we'll keep 'windowStart'
  windowStart = Math.max(windowStart, charIndexMap[rightChar] + 1);

However, the application of the max function seems to be unnecessary, i.e. if we change the code to

  windowStart = charIndexMap[rightChar] + 1;

it produces the same result.

This is because charIndexMap[rightChar] + 1 is guaranteed to be larger than windowStart.

A similar question was asked before, but it was left without the answer, so I decided to try my luck as well.

Run your code with input “abba” and you’ll get the answer

1 Like