javascript solution using set


  • 0
    K
    var lengthOfLongestSubstring = function(s) {
        let n = s.length;
        let set = new Set(),
            i = j = ans = 0;
        while (i < n && j < n) {
            if (!set.has(s.charAt(j))) {
              set.add(s.charAt(j++));
              ans = Math.max(ans, set.size);
            } else {
              set.delete(s.charAt(i++));
            }
        }
        return ans;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.