All files / leetCode 0003.ts

100% Statements 14/14
100% Branches 6/6
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  4x 1x   3x     3x 3x       19x 19x 19x 11x 19x 19x     3x    
export default function lengthOfLongestSubstring(s: string): number {
  if (s.length === 0)
    return 0
 
  let maxLen = 0
 
  // 最大子串为 2 个重复字符间字符串长度 + 1
  for (
    let lo = 0, hi = 0, map = new Map<string, number>();
    hi < s.length;
    hi++
  ) {
    const ch = s.charAt(hi)
    const index = map.get(ch)
    if (index !== undefined && lo <= index)
      lo = index + 1
    map.set(ch, hi)
    maxLen = Math.max(maxLen, hi - lo + 1)
  }
 
  return maxLen
}