๋ฌธ์
ํ์ด ๊ณผ์
์ค๋ณต๋ ๋ฌธ์๊ฐ ์๋ ๋ถ๋ถ ๋ฌธ์์ด ์ค์์ ๊ฐ์ฅ ๊ธธ์ด๊ฐ ๊ธด ๊ฒ์ ์ฐพ๋ ๋ฌธ์ ์
๋๋ค.
๋ ๊ฐ์ ํฌ์ธํฐ head, tail
๋ฅผ ํ์ฉํด์ ์๋์ฐ๋ฅผ ๋ํ๋ด๊ณ ์ค๋ณต๋ ๋ฌธ์๊ฐ ๋ฐ๊ฒฌ๋๋ฉด
ํ์ฌ ์๋์ฐ์์ ์ค๋ณต๋ ๋ฌธ์์ ์์น๋ก head
๋ฅผ ์ด๋์ํค๋ฉด ๋ฉ๋๋ค.
์ฝ๋
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
const cache = new Map();
let head = 0;
let tail = 0;
let answer = 0;
for (; tail < s.length; tail++) {
if (cache.has(s[tail])) {
const duplicateIndex = cache.get(s[tail]);
for (let i = head; i < duplicateIndex; i++) {
cache.delete(s[i]);
}
answer = Math.max(answer, tail - head);
head = duplicateIndex + 1;
}
cache.set(s[tail], tail);
}
answer = Math.max(answer, tail - head);
return answer;
};
๋ฐ์ํ
'๐ algorithm > leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
LeetCode 494 - Target Sum (Medium) (0) | 2021.03.04 |
---|---|
LeetCode 187 - Repeated DNA Sequences (Medium) (0) | 2021.03.03 |
LeetCode 89 - Gray Code (Medium) (0) | 2021.03.03 |
LeetCode 118 - Pascal's Triangle (Easy) (0) | 2021.03.03 |
LeetCode 19 - Remove Nth Node From End of List (Medium) (0) | 2021.03.03 |
๐ฌ ๋๊ธ