Untitled
4 years ago in Plain Text
var TEST = function () {
let _savePair;
function _compareAndUpdate(newPair) {
let newLen = newPair[1] - newPair[0] + 1;
if (newLen > _savePair.len) {
_savePair.pair = newPair;
_savePair.len = newLen;
}
}
return {
exec: function (str) {
let len = str ? str.length : 0;
if (!len) { return 0; }
_savePair = { pair: [0, 0], len: 1 };
let map = new Map();
let contPair = [0, 0];
map.set(str[0], 0);
for (let i = 1; i < len; ++i) {
let theChar = str[i];
if (contPair[0] <= map.get(theChar)) {
_compareAndUpdate([ contPair[0], i - 1 ]);
contPair[0] = map.get(theChar) + 1;
}
contPair[1] = i;
map.set(theChar, i);
}
_compareAndUpdate(contPair);
return _savePair.len;
}
};
}();