Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, 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.
1 /** 2 * @param {string} s 3 * @return {number} 4 */ 5 var lengthOfLongestSubstring = function(s) { 6 7 var len = s.length, 8 local =1; 9 max = 1, 10 start = 0; 11 12 //处理""; 13 if(!len){ 14 return 0; 15 } 16 17 18 for(var end = 1;end < len;end++){ 19 20 var c = s.charAt(end); 21 var pattern = s.substring(start,end); 22 var index = pattern.indexOf(c); 23 24 if(index !== -1){ 25 26 local = end - start - index; 27 start = start + index + 1; 28 }else{ 29 local += 1; 30 max = local > max ? local : max; 31 } 32 33 } 34 return max; 35 };