{"record":{"id":"948376682d6fbc42","repo":"TheAlgorithms/Python","slug":"the-string-should-be-not-empty-string","errorCode":null,"errorMessage":"the string should be not empty string","messagePattern":"the string should be not empty string","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/word_break.py","lineNumber":59,"sourceCode":"        ...\n    ValueError: the string should be not empty string\n    >>> word_break('', ['a'])\n    Traceback (most recent call last):\n        ...\n    ValueError: the string should be not empty string\n    >>> word_break('abc', [123])\n    Traceback (most recent call last):\n        ...\n    ValueError: the words should be a list of non-empty strings\n    >>> word_break('abc', [''])\n    Traceback (most recent call last):\n        ...\n    ValueError: the words should be a list of non-empty strings\n    \"\"\"\n\n    # Validation\n    if not isinstance(string, str) or len(string) == 0:\n        raise ValueError(\"the string should be not empty string\")\n\n    if not isinstance(words, list) or not all(\n        isinstance(item, str) and len(item) > 0 for item in words\n    ):\n        raise ValueError(\"the words should be a list of non-empty strings\")\n\n    # Build trie\n    trie: dict[str, Any] = {}\n    word_keeper_key = \"WORD_KEEPER\"\n\n    for word in words:\n        trie_node = trie\n        for c in word:\n            if c not in trie_node:\n                trie_node[c] = {}\n\n            trie_node = trie_node[c]\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/word_break.py#L41-L77","documentation":"Raised by word_break() when the target string is not a str instance or is empty. The algorithm builds a trie character-by-character over the input, so an empty string leaves nothing to match and non-string input would break iteration. This check runs before the words validation.","triggerScenarios":"Calling word_break('', words) or word_break(None, words) or word_break(123, ['1','2']). A string of only spaces (' ') does NOT trigger it — only zero length or non-str types do.","commonSituations":"Passing a stripped/filtered value that became empty (e.g. blank line from a file); a variable that is None after a failed parse; feeding bytes instead of str in Python 3.","solutions":["Guard the call: if not string: skip or handle the empty case in your caller.","Ensure the value is str: decode bytes with .decode('utf-8') and coerce with str() only when appropriate.","Treat an empty target as a no-op (trivially breakable) rather than an error in your own flow."],"exampleFix":"# before\nword_break('', ['a', 'b'])  # ValueError\n\n# after\nresult = True if not string else word_break(string, ['a', 'b'])","handlingStrategy":"validation","validationCode":"def valid_target(string: object) -> bool:\n    return isinstance(string, str) and len(string) > 0","typeGuard":"def non_empty_str(s: object) -> TypeGuard[str]:\n    return isinstance(s, str) and len(s) > 0","tryCatchPattern":"try:\n    word_break(string, words)\nexcept ValueError as e:\n    if 'not empty string' in str(e):\n        return True  # empty string trivially breaks\n    raise","preventionTips":["Skip blank lines/inputs before processing.","Decode bytes to str at the I/O boundary.","Treat an empty target as a trivial case in your caller."],"tags":["dynamic-programming","input-validation","strings"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}