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.
解題思路
1.用新的字串存取不重複的字元
2.利用迴圈跑取輸入字串
3.回傳不重複字符串的最大長度
CODE
var lengthOfLongestSubstring = function(s) {
let arr="";
let max=0;
for(var i=0;i<s.length;i++)
{
let index=arr.indexOf(s[i]);
arr+=s[i];
if(index!=-1)
{
arr=arr.substring(index+1);
}
max=Math.max(max,arr.length);
}
return max;
}