파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 6. 10. 23:45

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

Longest Substring Without Repeating Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

어제에 이어서 오늘도 Two Pointer 문제.
지난주 스터디에서 작성했던 소스 참고해서 내 힘으로 품!! 오예~~
(5번 WA 뜬 건 안 비밀.....)

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        ar = [0] * 128
        l = len(s)
        left, right = 0, 0
        res = 0
        
        if l == 0:
            return 0
        if l == 1:
            return 1
        
        while left <= right:            
            if ar[ord(s[right])] == 0:
                ar[ord(s[right])] += 1
                right += 1
            elif ar[ord(s[right])] > 0:
                ar[ord(s[left])] -= 1
                left += 1                
            res = max(res, right-left)
            
            if right == l:
                break                
        return res
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.06.12
Today's Challenge  (0) 2022.06.11
Today's Challenge  (0) 2022.06.09
Today's Challenge  (0) 2022.06.08
Today's Challenge  (0) 2022.06.07
Comments