-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27e2dd7
commit 5b1c79d
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
longest-substring-without-repeating-characters/hwanmini.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|