function longeststr(s){
let str='';//当前最长字串
let lens=0;
let l=s.length
for(let i=0;i<l;i++){
char=s.charAt(i)
index=str.indexOf(char)
if(index===-1){
str+=char
lens=lens<str.length?str.length:lens
}
else{
str=str.substr(index+1)+char
}
}
return lens;
}
console.log(longeststr('abcdeafghh'))
思路:
1.for循环