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

longestNRS("abcabcbb") ➞ "abc"

longestNRS("aaaaaa") ➞ "a"

longestNRS("abcde") ➞ "abcde"

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