# # Longest Substring Without Repeating Characters

Medium

Given a string, find the length of the longest substring without repeating characters.

## # Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.


## # Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.


## # Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


## # Solution

• 遍历字符串，获取当前索引下的字符
• 从存放无重复子串的字符串中搜索当前字符
• 搜索到，则截取
• 没有搜索到则直接加入到存放无重复子串的字符串
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let size = 0 // 用于存放当前最长无重复子串的长度
let str = '' // 用于存放无重复子串
const len = s.length;
for(let i = 0; i < len; i++) {
let char = s.charAt(i)
let index = str.indexOf(char)
if(index === -1) {
str += char
size = size < str.length ? str.length : size
} else {
str = str.substr(index + 1) + char
}
}
return size
};