Longest Substring with Distinct Characters

Problem Statement

Given a string, find the length of the longest substring, which has all distinct characters.

Example:

Input: String="aabccbb"
Output: 3
Explanation: The longest substring with distinct characters is "abc".

Solution

const func = (str) => {
// Declare left pointer, max var, and storage hash map.
  let left = 0;
  let max = 0;
  let storage = {};
  for (let right = 0; right < str.length; right++) {
// set rightLetter to current string for convenience.
    const rightLetter = str[right];
// cannot do if (storage[rightLetter]) because that may    evaluate to 0.
    if (rightLetter in storage) {
// If we see this character in storage, that means we've seen a repeat and must begin a new subsequence.
// To do so, set left to whatever rightLetter's index was + 1.
      left = storage[rightLetter] + 1;
    }
// Update storage at rightLetter's value to whatever the current index is.
    storage[rightLetter] = right;
// Update max.
    max = Math.max(max, right - left + 1);
  }
  return max;
};