leetCode 无重复字符的长子串

var lengthOfLongestSubstring = function (s) {
  if (!s) {
    return 0;
  }
  if (!s.trim()) {
    return 1;
  }

  let list = [];
  const source = s.split("");
  const len = source.length;
  let max = 1;
  let i = 0;

  while (i < len) {
    const str = source[i];

    if (!list.includes(str)) {
      list.push(str);
    } else {
      list.splice(0, list.indexOf(str) + 1);
      list.push(str);
    }
    i++;
    if (list.length > max) {
      max = list.length;
    }
  }

  return max;
};

console.log(3, lengthOfLongestSubstring("aabaab!bb"));
console.log(3, lengthOfLongestSubstring("abcabcbb"));
console.log(3, lengthOfLongestSubstring("pwwkew"));
console.log(3, lengthOfLongestSubstring("dvdf"));
console.log(1, lengthOfLongestSubstring("bbbb"));
console.log(1, lengthOfLongestSubstring(" "));