← Back to challenges

Longest Substring with Non-repeating Characters

PythonHardstringsloops

Instructions

Write a function that returns the longest non-repeating substring for a string input.

Examples

longest_nonrepeating_substring("abcabcbb") ➞ "abc"

longest_nonrepeating_substring("aaaaaa") ➞ "a"

longest_nonrepeating_substring("abcde") ➞ "abcde"

longest_nonrepeating_substring("abcda") ➞ "abcd"

Notes

  • If multiple substrings tie in length, return the one which occurs first.
  • Bonus: Can you solve this problem in linear time?
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.