0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

3. Longest Substring Without Repeating Characters

Posted at

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

CODE

def lengthOfLongestSubstring(s):
    left, max_length = 0, 0
    substring = set()
    for right in range(len(s)):
        if s[right] not in substring:
            substring.add(s[right])
            max_length = max(max_length, right - left + 1)
        else:
            # ifではなくwhile
            while s[right] in substring:
                substring.remove(s[left])
                left += 1
            # 結局、右要素をaddする
            substring.add(s[right])
            
    return max_length

set()で初期化ができて、addremoveで処理する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?