Longest Substring Without Repeating Characters - LeetCode 3 - JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ม.ค. 2025

ความคิดเห็น • 2

  • @mullayam
    @mullayam 5 หลายเดือนก่อน

    ```function longestSubString(s) {
    let result = 0;
    let leftpointer = 0;
    const visistedChars = new Map();
    for (let rightpointer = 0; rightpointer < s.length; rightpointer++) {
    while (visistedChars.has(s[rightpointer])) {
    visistedChars.delete(s[rightpointer]);
    leftpointer++;
    }
    let width = rightpointer - leftpointer + 1;
    result = Math.max(result, width);
    visistedChars.set(s[rightpointer]);
    }
    return result;
    }
    console.log(longestSubString("abcbcbdef"))``` it gives me 6

    • @CodeWithZi
      @CodeWithZi  5 หลายเดือนก่อน

      function longestSubString(s) {
      let result = 0;
      let leftpointer = 0;
      const visitedChars = new Map();
      for (let rightpointer = 0; rightpointer < s.length; rightpointer++) {
      while (visitedChars.has(s[rightpointer])) {
      visitedChars.delete(s[leftpointer]);
      leftpointer++;
      }
      visitedChars.set(s[rightpointer], rightpointer);
      let width = rightpointer - leftpointer + 1;
      result = Math.max(result, width);
      }
      return result;
      }
      there were some issues in your code, the above version should work. Hope it helps :)