3. Longest Substring Without Repeating Characters

2021. 10. 15. 03:36Leetcode

Description:

Given a string s, find the length of the longest substring without repeating characters.

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        slow=0
        fast=0
        long=0
        
        map={}
        
        while fast<len(s):
            if s[fast] in map:
                map[s[fast]]+=1
            else:
                map[s[fast]]=1
            
            if map[s[fast]]>1:
                long=max(long,fast-slow)
                
                while map[s[fast]]>1:
                    map[s[slow]]-=1
                    slow+=1
            
            fast+=1
            
        long=max(long,fast-slow)
        
        return long
            
# when n==len(str)
# T/C: O(n)
# S/C: O(n)

'Leetcode' 카테고리의 다른 글

661. Image Smoother  (0) 2021.10.17
73. Set Matrix Zeroes  (0) 2021.10.17
1155. Number of Dice Rolls With Target Sum  (0) 2021.10.16
931. Minimum Falling Path Sum  (0) 2021.10.16
1008. Construct Binary Search Tree from Preorder Traversal  (0) 2021.10.15