AlgoDaily Solution
1var assert = require('assert');
2
3var lengthOfLongestSubstring = function (s) {
4 var startNum = 0,
5 maximumLen = 0;
6 var map = new Map();
7
8 for (var i = 0; i < s.length; i++) {
9 var ch = s[i];
10
11 if (map.get(ch) >= startNum) startNum = map.get(ch) + 1;
12 map.set(ch, i);
13
14 if (i - startNum + 1 > maximumLen) maximumLen = i - startNum + 1;
15 }
16
17 return maximumLen;
18};
19
20try {
21 assert.equal(lengthOfLongestSubstring('algodaily'), 7);
22
23 console.log(
24 'PASSED: ' +
25 "`lengthOfLongestSubstring('algodaily')` should be equal to `7`"
26 );
27} catch (err) {
28 console.log(err);
29}
30
31try {
32 assert.equal(lengthOfLongestSubstring('thisisatest'), 5);
33
34 console.log(
35 'PASSED: ' +
36 "`lengthOfLongestSubstring('thisisatest')` should be equal to `5`"
37 );
38} catch (err) {
39 console.log(err);
40}
41
42try {
43 assert.equal(lengthOfLongestSubstring('rosesarered'), 4);
44
45 console.log(
46 'PASSED: ' +
47 "`lengthOfLongestSubstring('rosesarered')` should be equal to `4`"
48 );
49} catch (err) {
50 console.log(err);
51}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
40
var assert = require('assert');
var lengthOfLongestSubstring = function (s) {
// fill this in
return maximumLen;
};
try {
assert.equal(lengthOfLongestSubstring('algodaily'), 7);
console.log(
'PASSED: ' +
"`lengthOfLongestSubstring('algodaily')` should be equal to `7`"
);
} catch (err) {
console.log(err);
}
try {
assert.equal(lengthOfLongestSubstring('thisisatest'), 5);
console.log(
'PASSED: ' +
"`lengthOfLongestSubstring('thisisatest')` should be equal to `5`"
);
} catch (err) {
console.log(err);
OUTPUT
Results will appear here.