{"record":{"id":"fad26b39a2d5a061","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-type","errorCode":null,"errorMessage":"Invalid Input Type","messagePattern":"Invalid Input Type","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/LengthofLongestSubstringWithoutRepetition.js","lineNumber":12,"sourceCode":"/*\n * @description : Given a string, the function finds the length of the longest substring without any repeating characters\n * @param {String} str - The input string\n * @returns {Number} The Length of the longest substring in a given string without repeating characters\n * @example lengthOfLongestSubstring(\"abcabcbb\") => 3\n * @example lengthOfLongestSubstring(\"bbbbb\") => 1\n * @see https://leetcode.com/problems/longest-substring-without-repeating-characters/\n */\n\nconst lengthOfLongestSubstring = (s) => {\n  if (typeof s !== 'string') {\n    throw new TypeError('Invalid Input Type')\n  }\n  let maxLength = 0\n  let start = 0\n  const charMap = new Map()\n  for (let end = 0; end < s.length; end++) {\n    if (charMap.has(s[end])) {\n      start = Math.max(start, charMap.get(s[end]) + 1)\n    }\n    charMap.set(s[end], end)\n    maxLength = Math.max(maxLength, end - start + 1)\n  }\n  return maxLength\n}\n\nexport { lengthOfLongestSubstring }\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/LengthofLongestSubstringWithoutRepetition.js#L1-L28","documentation":"Guard in lengthOfLongestSubstring. The function computes the length of the longest substring without repeating characters using a sliding window + Map, and requires the input to be a string, throwing TypeError otherwise.","triggerScenarios":"Calling lengthOfLongestSubstring(undefined), lengthOfLongestSubstring(null), lengthOfLongestSubstring(12345), lengthOfLongestSubstring(['a','b','c']). Any input where typeof !== 'string'.","commonSituations":"A LeetCode input arriving as a number; a payload field that is null; passing an array of characters instead of a string.","solutions":["Pass a string; coerce numbers via String(n).","Default missing values to '' (returns 0).","Validate typeof at the boundary."],"exampleFix":"// before\nlengthOfLongestSubstring(maybeStr)\n\n// after\nlengthOfLongestSubstring(typeof maybeStr === 'string' ? maybeStr : String(maybeStr ?? ''))","handlingStrategy":"type-guard","validationCode":"if (typeof s !== 'string') {\n  throw new TypeError('s must be a string')\n}\nlengthOfLongestSubstring(s)","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try {\n  lengthOfLongestSubstring(input)\n} catch (e) {\n  if (e instanceof TypeError && /Invalid Input Type/i.test(e.message)) { /* not a string */ } else throw e\n}","preventionTips":["Coerce numbers via String().","Default missing values to '' (returns 0).","Validate typeof at the boundary."],"tags":["string","validation","type-guard","sliding-window","substring"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}