/ leetcodesolve / longeststring.py
swadhinbiswas update · 3/29/2024
1f22b99
11 lines · 341 Bytes
1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        l = answer = 0
        chars = [-1]*128
        for r, ch in enumerate(s):
            if chars[ord(ch)] >= l:
                l = chars[ord(ch)]+1
            else:
                answer = max(answer, r-l+1)
            chars[ord(ch)] = r
        return answer