Skip to Content

无重复字符的最长子串

https://leetcode.cn/problems/longest-substring-without-repeating-characters/

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

function lengthOfLongestSubstring(s: string): number { let max = 0; const dp: string[] = []; for (const c of s) { const index = dp.indexOf(c); if (index !== -1) { max = Math.max(max, dp.length); dp.splice(0, index + 1); } dp.push(c); } return Math.max(max, dp.length); }
Last updated on