Longest Substring with Non-repeating Characters

Published by Helen Yu in

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?
Watch a quick demo on how Edabit works.