{"record":{"id":"d0943efd1d3a6687","repo":"TheAlgorithms/Python","slug":"longest-common-substring-takes-two-strings-for-i","errorCode":null,"errorMessage":"longest_common_substring() takes two strings for inputs","messagePattern":"longest_common_substring\\(\\) takes two strings for inputs","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/longest_common_substring.py","lineNumber":44,"sourceCode":"    'bcd'\n    >>> longest_common_substring(\"abcdef\", \"xabded\")\n    'ab'\n    >>> longest_common_substring(\"GeeksforGeeks\", \"GeeksQuiz\")\n    'Geeks'\n    >>> longest_common_substring(\"abcdxyz\", \"xyzabcd\")\n    'abcd'\n    >>> longest_common_substring(\"zxabcdezy\", \"yzabcdezx\")\n    'abcdez'\n    >>> longest_common_substring(\"OldSite:GeeksforGeeks.org\", \"NewSite:GeeksQuiz.com\")\n    'Site:Geeks'\n    >>> longest_common_substring(1, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: longest_common_substring() takes two strings for inputs\n    \"\"\"\n\n    if not (isinstance(text1, str) and isinstance(text2, str)):\n        raise ValueError(\"longest_common_substring() takes two strings for inputs\")\n\n    if not text1 or not text2:\n        return \"\"\n\n    text1_length = len(text1)\n    text2_length = len(text2)\n\n    dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)]\n    end_pos = 0\n    max_length = 0\n\n    for i in range(1, text1_length + 1):\n        for j in range(1, text2_length + 1):\n            if text1[i - 1] == text2[j - 1]:\n                dp[i][j] = 1 + dp[i - 1][j - 1]\n                if dp[i][j] > max_length:\n                    end_pos = i\n                    max_length = dp[i][j]","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/longest_common_substring.py#L26-L62","documentation":"Raised by longest_common_substring(text1, text2) when either argument fails isinstance(x, str). The DP algorithm indexes characters of both inputs, so non-string arguments (ints, lists, None) are rejected up front with this ValueError. Empty strings are valid and return '' via the following check, not this error.","triggerScenarios":"longest_common_substring(1, 1) as in the doctest; passing bytes (b'abc') since bytes is not str; passing None when one input is missing; passing a list of characters instead of a joined string.","commonSituations":"Data read as bytes from files/networks in Python 3; optional fields that default to None; iterating characters into a list instead of using the string directly.","solutions":["Decode bytes before calling: longest_common_substring(a.decode(), b.decode()).","Coerce or default missing inputs: text1 = text1 or ''.","Join char lists: ''.join(chars) before the call."],"exampleFix":"# before\nlcs = longest_common_substring(payload_a, payload_b)  # bytes -> ValueError\n\n# after\nlcs = longest_common_substring(payload_a.decode('utf-8'), payload_b.decode('utf-8'))","handlingStrategy":"type-guard","validationCode":"if not isinstance(text1, str):\n    text1 = text1.decode() if isinstance(text1, bytes) else str(text1)\nif not isinstance(text2, str):\n    text2 = text2.decode() if isinstance(text2, bytes) else str(text2)\nresult = longest_common_substring(text1, text2)","typeGuard":"def is_str_pair(a: object, b: object) -> bool:\n    return isinstance(a, str) and isinstance(b, str)","tryCatchPattern":"try:\n    result = longest_common_substring(text1, text2)\nexcept ValueError as exc:\n    if 'two strings' in str(exc):\n        raise TypeError('both inputs must be str; decode bytes first') from exc\n    raise","preventionTips":["Decode bytes to str at I/O boundaries in Python 3.","Default optional string parameters to '' rather than None.","Pass strings directly instead of lists of characters."],"tags":["python","input-validation","string","dynamic-programming"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}