Skip to content

Commit

Permalink
improve sliding window logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyeon committed Jan 24, 2025
1 parent d642590 commit 0527960
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions longest-substring-without-repeating-characters/mmyeon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ function lengthOfLongestSubstring(s: string): number {

return maxLength;
}

// Map ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต ๋ฐœ์ƒ์‹œ start ์ธ๋ฑ์Šค๊ฐ€ ์ ํ”„ํ•˜๋„๋ก ๊ฐœ์„ 
function lengthOfLongestSubstring(s: string): number {
let start = 0,
maxLength = 0;
const map = new Map<string, number>();

for (let i = 0; i < s.length; i++) {
const char = s[i];
// ์ค‘๋ณต ์žˆ๋Š” ๊ฒฝ์šฐ, ์ค‘๋ณต๋ฌธ์ž์˜ ๋‹ค์Œ ์œ„์น˜๋กœ ์ ํ”„
if (map.has(char)) start = Math.max(start, map.get(char)! + 1);
// ์ธ๋ฑ์Šค ๊ฐฑ์‹ 
map.set(char, i);

maxLength = Math.max(maxLength, i - start + 1);
}

return maxLength;
}

0 comments on commit 0527960

Please sign in to comment.