educative.io

Solution using Medium implementation, Does this work (Should still be O(n))?

const non_repeat_substring = function(str) {
  let hash = {}
  let maxValue = 0
  let windowStart = 0

  for (i = 0; i < str.length; i++) {
    let rightChar = str[i]
    if (!hash[rightChar]) {
      hash[rightChar] = 0
    }
    hash[rightChar]++

    while (Object.values(hash).includes(2)) {
      let leftChar = str[windowStart]
      hash[leftChar]--
      windowStart++
    }

    maxValue = Math.max(maxValue, i - windowStart + 1)
  }

  return maxValue
};

This passes the tests, Does this work? It should still be O(n + n) aka O(n)??