Given a string s , please find out the length of the longest substring that does not contain repeated characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: Because the longest substring without repeated characters is "abc", its length is 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: Because the longest substring without repeated characters is "b", its length is 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: Because the longest substring without repeated characters is "wke", its length is 3.
Please note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.

Example 4:

Input: s = ""
Output: 0

Problem-solving ideas

solution one: maintain the array

Use an array to maintain the sliding window.

Traverse the string to determine whether the character is in the array of the sliding window:

  • If not, push enters the array;
  • Then delete the same character and the character before the same character in the sliding window array, and then add the current character push into the array;
  • Then max to the length of the current longest string;
function lengthOfLongestSubstring(s) {
    let arr = [], max = 0;
    for (let i=0; i<s.length; i++) {
        let index = arr.indexOf(s[i]);
        if (index != -1) {
            // 当前字符在滑动窗口的数组里
            // 把数组里面相同字符及前面的字符全部都删掉
            arr.splice(0, index + 1);
        }
        arr.push(s[i]);
        max = Math.max(arr.length, max);
    }
    return max;
}
Time complexity: O(n^2) , wherein arr.indexOf() time complexity is O(n) , arr.splice(0, index + 1) time complexity is also O(n)
Space complexity: O(n)

solution two: maintain the subscript

Similar to the above solution, but use subscripts to maintain the sliding window.

Use i to mark the starting subscript of the j string, and 061397fc2bb082 for the current traverse character subscript.

function lengthOfLongestSubstring(s) {
    let index = 0, max = 0;
    for (let i=0, j=0; j<s.length; j++) {
        index = s.substring(i, j).indexOf(s[j]);
        if (index != -1) {
            i = i + index + 1;
        }
        max = Math.max(max, j - i + 1);
    }
    return max;
}
Time complexity: O(n^2)
Space complexity: O(n)

Solution Three: Optimized Map

Use map to store the current characters that have been traversed, key for characters, and value for subscripts.

Use i to mark the starting subscript of the j string, and 061397fc2bb138 as the current traverse character subscript.

Traverse the string to determine whether the current character already map . If it exists, update the starting subscript of the non-repeated string i to the next position of the same string. At this time, from i to j is the latest non-repeated string, update max , put the current character and subscript into map .

Finally, return to map .

function lengthOfLongestSubstring(s) {
    let map = new Map(), max = 0;
    for (let i=0, j=0; j<s.length; j++) {
        if (map.has(s[j])) {
            // 更新起始下标为相同字符串的下一位置
            i = Math.max(map.get(s[j]) + 1, i);
        }
        max = Math.max(max, j - i + 1);
        map.set(s[j], j);
    }
    return max;
}
Time complexity: O(n)
Space complexity: O(n)

一杯绿茶
199 声望17 粉丝

人在一起就是过节,心在一起就是团圆