← Back to challenges

Longest Substring with Non-repeating Characters

JavaScriptHardstringsloops

Instructions

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?
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.