Skip to content
DeepForgeby svx
All problems
HardAlgorithmsal-040

Longest Unique Substring

Problem

Return the length of the longest substring of s that contains no repeated characters.

Use a sliding window with a dictionary storing each character's last index; when a repeat falls inside the current window, move the window start to just past the previous occurrence. Return 0 for the empty string.

Hint

Only move the start forward when the previous occurrence is still inside the window.

Test cases

case 1
in: ["abcabcbb"]
expected: 3
case 2
in: ["bbbbb"]
expected: 1
case 3
in: [""]
expected: 0
case 4
in: ["pwwkew"]
expected: 3
case 5
in: ["dvdf"]
expected: 3
Starter codePython
def length_of_longest_unique_substring(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor