Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 코딩
- 백준
- boj
- 완전탐색
- 방송대
- 리트코드
- 투포인터
- 방통대
- 방송통신대학교
- two pointers
- greedy
- Binary Search
- java
- algorithm
- it
- Git
- sliding window
- 컴퓨터과학과
- 이진탐색
- DP
- 그리디
- 깃
- 알고리즘
- dynamic programming
- 코테
- 자바
- 자바스크립트
- 탐욕알고리즘
- javascript
- LeetCode
Archives
- Today
- Total
개발이 취미인 주니어 기획자
[슬라이딩윈도우][JavaScript][LeetCode] #3. Longest Substring Without Repeating Characters 본문
문제 풀이/알고리즘 문제 풀이
[슬라이딩윈도우][JavaScript][LeetCode] #3. Longest Substring Without Repeating Characters
큐 2023. 4. 5. 11:00728x90
반응형
#슬라이딩 윈도우 #MEDIUM
Longest Substring Without Repeating Characters - LeetCode
Can you solve this real interview question? Longest Substring Without Repeating Characters - Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "
leetcode.com
🌷 문제 설명
✏️ LeetCode 연습문제: Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
🎃 제한 사항
0 <= s.length <= 5 * 10^4
s consists of English letters, digits, symbols and spaces.
입출력 예
| s | result |
| "abcabcbb" | 3 |
| "bbbbb" | 1 |
| "pwwkew" |
3 |
🌷 내 코드
1. 셋을 사용한 풀이: Set

var lengthOfLongestSubstring = function (s) {
let set = new Set();
let left = 0;
let maxSize = 0;
// s의 길이가 0이면 0을, 1이면 1을 반환한다
if (s.length === 0) {
return 0;
} else if (s.length === 1) {
return 1;
}
for (let i = 0; i < s.length; i++) {
while (set.has(s[i])) {
//2. set에 다음 알파벳이 있다면, 즉 중복된다면
set.delete(s[left]); //젤 왼쪽에 있는 값을 지워주고
left++; // left 인덱스를 올려줍니다
}
set.add(s[i]); //1. set에 알파벳 하나씩을 순서대로 넣기
maxSize = Math.max(maxSize, i - left + 1); //최댓값구하기: i, left 둘 다 인덱스라 1을 더해줌
}
return maxSize;
};
2. 배열을 사용한 풀이: Array

var lengthOfLongestSubstring = function(s) {
let arr = new Array()
let left = 0
let maxSize = 0
if (s.length === 0) {
return 0
} else if (s.length === 1) {
return 1
}
for (let i=0; i < s.length; i++) {
while (arr.includes(s[i])) {
arr.shift()
left++
}
arr.push(s[i])
maxSize = Math.max(maxSize, i-left+1)
}
return maxSize
};
🌷 코멘트
set과 array 똑같이 가능인데 Set 했을 때랑 Array 했을 때랑 속도 차이가 좀 남!
대신 Array로 했을 때 메모리를 덜 쓰는 경향이 있는 듯?
블로그 내용에 문제가 있다면 댓글 혹은 아래로 연락주세요!
~대가리 꽃밭인 디지털 노마드가 꿈이예요~
🧚♀️ Gyumin Lee
📧 gyumin.q.lee@gmail.com
qminlee723 - Overview
noob. qminlee723 has 8 repositories available. Follow their code on GitHub.
github.com
728x90
반응형
'문제 풀이 > 알고리즘 문제 풀이' 카테고리의 다른 글
| [슬라이딩윈도우][JavaScript][LeetCode] #567. Permutation in String (0) | 2023.04.07 |
|---|---|
| [투포인터][JavaScript][LeetCode] #19. Remove Nth Node From End of List (0) | 2023.04.06 |
| [투포인터][JavaScript][LeetCode] #876. Middle of the Linked List (0) | 2023.04.03 |
| [투포인터][JavaScript][LeetCode] #167. Two Sum II - Input Array Is Sorted (0) | 2023.04.02 |
| [투포인터][JavaScript][LeetCode] #1480. Running Sum of 1d Array (0) | 2023.04.02 |