HZFE / algorithms-solving

1 stars 0 forks source link

2022-09-14 #47

Open github-actions[bot] opened 2 years ago

gongpeione commented 2 years ago

3 Longest Substring Without Repeating Characters

/*
 * @lc app=leetcode id=3 lang=typescript
 *
 * [3] Longest Substring Without Repeating Characters
 */

// @lc code=start
function lengthOfLongestSubstring(s: string): number {
    let max = 0;
    let l = 0;
    let slideWindow = [] as string[];

    for (let r = 0; r < s.length; r++) {
        // move left to the position that s[r] is not included in the slideWindow
        while (slideWindow.includes(s[r])) {
            slideWindow.shift();
            l++;
        }
        // push current right element to the slideWindow
        slideWindow.push(s[r]);
        // compare to the current max value with slideWindow's length
        max = Math.max(max, slideWindow.length);
    }

    return max;
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms