Skip to content

Commit

Permalink
feat: 문제풀이 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JEONGHWANMIN committed Sep 26, 2024
1 parent 27e2dd7 commit 5b1c79d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let maxCount = 0;
const map = new Map()

let leftIdx = 0;
for (let rightIdx = 0 ; rightIdx < s.length; rightIdx++) {
const char = s[rightIdx]
if (map.has(char) && map.get(char) >= leftIdx) leftIdx = map.get(char) + 1;
map.set(char, rightIdx)
maxCount = Math.max(maxCount, rightIdx - leftIdx + 1)
}

return maxCount
};


console.log(lengthOfLongestSubstring("abcabcbb"))
console.log(lengthOfLongestSubstring("bbbbb"))
console.log(lengthOfLongestSubstring("pwwkew"))
console.log(lengthOfLongestSubstring("abba"))

0 comments on commit 5b1c79d

Please sign in to comment.