Longest Substring with Non-repeating Characters

Published by Matt in

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

Examples

longestNonrepeatingSubstring("abcabcbb") ➞ "abc"

longestNonrepeatingSubstring("aaaaaa") ➞ "a"

longestNonrepeatingSubstring("abcde") ➞ "abcde"

longestNonrepeatingSubstring("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.