Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Sep 10, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 리트코드 3번: 중복 문자 없는 최장 부분 문자열 문제 해결

  • 투 포인터와 Set을 활용한 효율적인 구현

  • 엣지 케이스 처리와 상세한 한글 주석 포함


@github-actions
Copy link

PR Reviewer Guide 🔍

🧪 PR contains tests
⚡ Recommended focus areas for review

성능 최적화

현재 구현은 O(2n) 시간 복잡도를 가집니다. 내부 while 루프를 제거하고 Map을 사용하여 O(n)으로 최적화할 수 있습니다. Map에 문자의 마지막 위치를 저장하면 left 포인터를 즉시 이동할 수 있습니다.

while (charSet.has(s[right]) && left < right) {
  charSet.delete(s[left]);
  left++;
}
엣지케이스 처리

모든 문자가 동일한 문자열(예: "aaaaa")이나 모든 문자가 연속으로 반복되는 문자열(예: "abababab")과 같은 엣지케이스에서 불필요한 반복이 발생할 수 있습니다. 이러한 케이스들에 대한 초기 검사를 추가하면 성능을 개선할 수 있습니다.

while (left < right && right < s.length) {
  // right의 문자가 charSet에 없을 때 까지 까지 left를 이동
  while (charSet.has(s[right]) && left < right) {
    charSet.delete(s[left]);
    left++;
  }
  // substring 길이 업데이트하기
  maxLength = Math.max(maxLength, right - left + 1);
  // right를 오른쪽으로 한칸 옮기기
  charSet.add(s[right]);
  right++;
}
메모리 최적화

Set 대신 길이 128의 boolean 배열(ASCII 문자 기준)을 사용하면 메모리 사용량을 줄일 수 있습니다. 또한 빈번한 add/delete 연산을 줄일 수 있어 성능도 향상됩니다.

const charSet = new Set();

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
슬라이딩 윈도우 로직 개선

슬라이딩 윈도우 로직을 더 명확하게 개선하고 변수명을 의미있게 변경하세요. windowStartwindowEnd를 사용하여 윈도우의 시작과 끝을
나타내고, 중복 문자 처리 로직을 단순화했습니다.

Leetcode/3.longest-substring-without-repeating-characters.ts [9-26]

-let maxLength = 1; // 최소 substring 길이는 문자 하나 길이인 1
-const charSet = new Set();
-let left = 0,
-  right = left + 1;
+let maxLength = 1;
+const seen = new Set();
+let windowStart = 0;
+let windowEnd = 0;
 
-charSet.add(s[left]);
-while (left < right && right < s.length) {
-  // right의 문자가 charSet에 없을 때 까지 까지 left를 이동
-  while (charSet.has(s[right]) && left < right) {
-    charSet.delete(s[left]);
-    left++;
+while (windowEnd < s.length) {
+  while (seen.has(s[windowEnd])) {
+    seen.delete(s[windowStart]);
+    windowStart++;
   }
-  // substring 길이 업데이트하기
-  maxLength = Math.max(maxLength, right - left + 1);
-  // right를 오른쪽으로 한칸 옮기기
-  charSet.add(s[right]);
-  right++;
+  seen.add(s[windowEnd]);
+  maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
+  windowEnd++;
 }
Suggestion importance[1-10]: 7

__

Why: 변수명을 windowStart/windowEnd로 변경하고 로직을 단순화하여 코드의 가독성과 유지보수성이 향상됩니다. 중복 문자 처리 로직도 더 간결해졌습니다.

Medium

@uyeon0 uyeon0 added leetcode Leetcode 문제 풀이 and removed Enhancement labels Sep 10, 2025
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Sep 10, 2025
@yoouyeon yoouyeon added ready-to-merge pr을 머지해주세요 and removed ready-to-merge pr을 머지해주세요 labels Sep 10, 2025
@yoouyeon yoouyeon added ready-to-merge pr을 머지해주세요 and removed ready-to-merge pr을 머지해주세요 labels Sep 10, 2025
@uyeon0 uyeon0 merged commit 4a0777b into main Sep 10, 2025
5 checks passed
@uyeon0 uyeon0 deleted the upload branch September 12, 2025 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

leetcode Leetcode 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants