https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/
For the above problem when I run the code in the terminal it is successfull all the times.
But when I submit it it says failure for the input bbbbb.
But the mase input when I run in the right hand Testcase it is successfull.
I have ran en extensive list of input and it always says successfull.
Below is my solution.
Let me know where is the problem.
`
var getMax = function getMax(a, b) {
return a > b ? a : b;
};
var maxLength = 0;
var maxLengthHashes = Object.create(null);
var lengthOfLongestSubstring = function (s) {
if (typeof s !== 'string' || s.length === 0) {
return 0;
}
const len = s.length;
if (len === 1 || maxLengthHashes[s] || maxLength > len) return 0;
let start = 0;
let end = 0;
let substring = '';
let ilen = 0;
let tempArr = [];
let curChar = '';
let countRepeatChars = 0;
let hash = Object.create(null);
const dupHash = Object.create(null);
for (var i = 0; i < len; i++) {
curChar = s[i];
if (hash[curChar]) {
countRepeatChars++;
hash[curChar].push(i);
dupHash[curChar] = hash[curChar];
} else {
hash[curChar] = [i];
}
}
hash = {};
if (!countRepeatChars) return len;
if (countRepeatChars === len - 1) return getMax(maxLength, 1);
for (key in dupHash) {
tempArr = dupHash[key];
ilen = tempArr.length;
for (var i = 0; i < ilen; i++) {
start = i ? (tempArr[i - 1] + 1) : 0;
end = (i === ilen - 1) ? (len - 1) : tempArr[i + 1] - 1;
substring = s.substring(start, end + 1);
if (maxLength >= substring.length) continue;
maxLengthHashes[substring] = lengthOfLongestSubstring(substring);
maxLength = getMax(maxLength, maxLengthHashes[substring]);
}
}
return maxLength;};
`