function lengthOfLongestSubstring(s: string) {
let tempArr: any[] = [];
let calculateBiggestLengthArray: any[] = [];
for (let i = 0; i < s.length; i++) {
for (let j = i; j < s.length; j++) {
if (!tempArr.includes(s[j])) {
tempArr.push(s[j]);
if (j === s.length - 1) {
calculateBiggestLengthArray.push(tempArr);
tempArr = []
}
} else {
calculateBiggestLengthArray.push(tempArr)
tempArr = [];
break;
}
}
}
//now calculate first biggest length element
let tempArrForMaxLength = []
for (let z = 0; z < calculateBiggestLengthArray.length; z++) {
tempArrForMaxLength.push(calculateBiggestLengthArray[z].length)
}
const index = tempArrForMaxLength.indexOf(Math.max(...tempArrForMaxLength) ?? 0)
return calculateBiggestLengthArray[index].length;
}Yes i have checked there is no chance of 'undefined' behavior.
Code works with my own (vscode and typescript playground) code & passes leetcode test cases, but when I submit I get:
Runtime Error Message:
return calculateBiggestLengthArray[index].length;
^
TypeError: Cannot read property 'length' of undefined
Line 30: Char 50 in solution.ts (lengthOfLongestSubstring)
Line 42: Char 19 in solution.ts (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 33: Char 26 in solution.ts (Object.<anonymous>)
Line 1251: Char 30 in loader.js (Module._compile)
Line 1272: Char 10 in loader.js (Object.Module._extensions..js)
Line 1100: Char 32 in loader.js (Module.load)
Line 962: Char 14 in loader.js (Function.Module._load)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Line 17: Char 47 in run_main_module.js
Last executed input:Here is another example of code that runs on vscode and fails on leetcode with the same type of error:
https://leetcode.com/problems/length-of-last-word/discuss/21885/javascript-runtime-error-message-line-12-typeerror-cannot-read-property-length-of-undefined