fueanta

LeetCode 03: Longest Substring Without Repeating Characters

Jul 7th, 2021
1,171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lengthOfLongestSubstring(s: string): number {
  2.     let start = 0, len = 0;
  3.     const charMap = new Map<string, number>();
  4.  
  5.     for (let i = 0; i < s.length; i++) {
  6.         const char = s.charAt(i);
  7.  
  8.         if (charMap.has(char)) {
  9.             if (charMap.get(char)! >= start) {
  10.                 start = charMap.get(char)! + 1;
  11.             }
  12.         }
  13.  
  14.         len = Math.max(len, i - start + 1);
  15.         charMap.set(char, i);
  16.     }
  17.  
  18.     return len;
  19. };
RAW Paste Data