물음표 살인마의 개발블로그

알고리즘 문제/Leetcode

3. Longest Substring Without Repeating Characters

BEstyle 2022. 12. 28. 00:36

Given a string s, find the length of the longest 

substring

 without repeating characters.

 

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if len(s)<=1:
            return len(s)
        lp=0
        rp=0
        longest=""
        clongest=""
        while rp<len(s):
            if s[rp] not in clongest:    
                clongest+=s[rp]
                rp+=1
            else:
                if len(longest)<len(clongest):
                    longest=clongest
                clongest = clongest[clongest.index(s[rp])+1:]
        return max(len(longest),len(clongest))

'알고리즘 문제 > Leetcode' 카테고리의 다른 글

682. Baseball Game  (0) 2022.12.28
844. Backspace String Compare  (2) 2022.12.28
283. Move Zeroes  (0) 2022.12.28
88. Merge Sorted Array  (0) 2022.12.27
1984. Minimum Difference Between Highest and Lowest of K Scores  (0) 2022.12.27