LeetCode 3. Longest Substring Without Repeating Characters

개발공부를해보자·2025년 1월 19일

LeetCode

목록 보기
30/95

파이썬 알고리즘 인터뷰 문제 30번(리트코드 3번) Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/

나의 풀이

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        last_index = dict()
        length_of_longest_substring = 0
        left = 0

        for right, char in enumerate(s):
            if char in last_index and last_index[char] >= left:
                left = last_index[char] + 1
            length_of_longest_substring = max(length_of_longest_substring, right - left + 1)
            last_index[char] = right
        
        return length_of_longest_substring
  • two pointers 이용한 간단한 풀이
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글