{"record":{"id":"dd8dc2811597cbaa","repo":"TheAlgorithms/Python","slug":"the-words-should-be-a-list-of-non-empty-strings","errorCode":null,"errorMessage":"the words should be a list of non-empty strings","messagePattern":"the words should be a list of non-empty strings","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/word_break.py","lineNumber":64,"sourceCode":"    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\n        trie_node[word_keeper_key] = True\n\n    len_string = len(string)\n\n    # Dynamic programming method","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/word_break.py#L46-L82","documentation":"Raised by word_break() when words is not a list or any element is not a non-empty string. The dictionary is inserted into a trie keyed by individual characters, so empty strings would create degenerate nodes and non-str elements would break char iteration. Both wrong-container and wrong-element cases produce this single message.","triggerScenarios":"Calling word_break('abc', ['']) or word_break('abc', [123]) or word_break('abc', 'abc') (a bare string is not a list); a tuple of words also fails since isinstance(words, list) is checked strictly.","commonSituations":"Deduplicating words into a set or tuple and passing it directly; loading a wordlist where blank lines become '' after .strip(); mixed-type lists from untyped JSON input.","solutions":["Normalize to a list of non-empty strings: words = [w for w in words if isinstance(w, str) and w].","Wrap sets/tuples: word_break(s, list(words)) after filtering empties.","Sanitize file-loaded wordlists by stripping and dropping blank lines."],"exampleFix":"# before\nword_break('abc', {'a', 'b', ''})  # ValueError (set + empty string)\n\n# after\nword_break('abc', [w for w in ['a', 'b', ''] if w])","handlingStrategy":"validation","validationCode":"def valid_words(words: object) -> bool:\n    return isinstance(words, list) and all(\n        isinstance(w, str) and len(w) > 0 for w in words\n    )","typeGuard":"def word_list(words: object) -> TypeGuard[list[str]]:\n    return isinstance(words, list) and all(\n        isinstance(w, str) and w for w in words\n    )","tryCatchPattern":null,"preventionTips":["Wrap sets/tuples with list(...) before calling.","Filter blank entries when loading wordlists: [w for w in raw if w.strip()].","Keep dictionary data as a list[str] end to end."],"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"}