릿코드 3. longest substring without repeating characters

전종원·2025년 9월 29일

Intuition

주어진 문자열에서 반복되지 않는 최장 부분문자열의 길이 출력

Approach

  • 문자열을 순회하며 각 문자가 큐에 있는지 큐가 비어있지 않은지 확인하여 큐가 비어있지 않고 문자가 큐에 있다면 계속 popleft() -> 중복제거됨
  • 큐에 문자 삽입 후 answer 업데이트

Complexity

  • Time complexity: O(n2)O(n^2)

  • Space complexity: O(n)O(n)

Code

from collections import deque

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        q = deque()
        start = 0
        answer = 0
        for e in range(len(s)):
            while q and s[e] in q:
                q.popleft()
            q.append(s[e])
            answer = max(answer, len(q))
        
        return answer

0개의 댓글