strings Flashcards

(1 cards)

1
Q
  1. Longest Substring Without Repeating Characters

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

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

A

def longestSubstring(string):

charSet = set()

left = 0
res = 0

for right in range(len(string)):
    if string[right] in charSet:
        charSet.remove(string[left])
        left += 1

    charSet.add(string[right])
    res = max(res, right - left + 1)
return res

string = ‘abcabcbb’

print(longestSubstring(string))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly