Edabit

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?

Solution:

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
function longestNonrepeatingSubstring(str) { 	let sol = "" 	for(let i = 0;i<=str.length;i++){ 		for(let j = 0;j<=str.length;j++){ 			let sbStr = str.substr(i,j); 			console.log(sbStr) 			let set = new Set([...sbStr]); 			if(sbStr.length === set.size && sol.length < sbStr.length){ 				sol = sbStr; 			} 		} 	} 	//console.log(sol) 	return sol; } 

This free site is ad-supported. Learn more