Skip to Main Content

Longest Substring Without Repeating Characters

Problem URL:Longest Substring Without Repeating Characters

My Solution

JavaScript

/**
 * @param {string} s
 * @return {number}
 */

const lengthOfLongestSubstring = (s) => {
  if (s.length < 1) return 0;

  let arr = [],
    index = -1,
    countArr = [];

  for (let i = 0; i < s.length; i++) {
    index = arr.indexOf(s[i]);
    if (index >= 0) {
      arr.splice(0, index + 1);
    }
    arr.push(s[i]);
    countArr.push(arr.length);
  }

  return Math.max(...countArr);
};

Let's Connect

Twitter GitHub LinkedIn