Return True if numbers have opposite signs False otherwise. >>> word_break("applepenapple", ["apple","pen"]) True >>> word_break("catsandog", ["cats","dog","sand","and","cat"]) False >>> word_break("cars", ["car","ca","rs"]) True >>> word_break('abc', []) False
(string: str, words: list[str])
| 25 | |
| 26 | |
| 27 | def word_break(string: str, words: list[str]) -> bool: |
| 28 | """ |
| 29 | Return True if numbers have opposite signs False otherwise. |
| 30 | |
| 31 | >>> word_break("applepenapple", ["apple","pen"]) |
| 32 | True |
| 33 | >>> word_break("catsandog", ["cats","dog","sand","and","cat"]) |
| 34 | False |
| 35 | >>> word_break("cars", ["car","ca","rs"]) |
| 36 | True |
| 37 | >>> word_break('abc', []) |
| 38 | False |
| 39 | >>> word_break(123, ['a']) |
| 40 | Traceback (most recent call last): |
| 41 | ... |
| 42 | ValueError: the string should be not empty string |
| 43 | >>> word_break('', ['a']) |
| 44 | Traceback (most recent call last): |
| 45 | ... |
| 46 | ValueError: the string should be not empty string |
| 47 | >>> word_break('abc', [123]) |
| 48 | Traceback (most recent call last): |
| 49 | ... |
| 50 | ValueError: the words should be a list of non-empty strings |
| 51 | >>> word_break('abc', ['']) |
| 52 | Traceback (most recent call last): |
| 53 | ... |
| 54 | ValueError: the words should be a list of non-empty strings |
| 55 | """ |
| 56 | |
| 57 | # Validation |
| 58 | if not isinstance(string, str) or len(string) == 0: |
| 59 | raise ValueError("the string should be not empty string") |
| 60 | |
| 61 | if not isinstance(words, list) or not all( |
| 62 | isinstance(item, str) and len(item) > 0 for item in words |
| 63 | ): |
| 64 | raise ValueError("the words should be a list of non-empty strings") |
| 65 | |
| 66 | # Build trie |
| 67 | trie: dict[str, Any] = {} |
| 68 | word_keeper_key = "WORD_KEEPER" |
| 69 | |
| 70 | for word in words: |
| 71 | trie_node = trie |
| 72 | for c in word: |
| 73 | if c not in trie_node: |
| 74 | trie_node[c] = {} |
| 75 | |
| 76 | trie_node = trie_node[c] |
| 77 | |
| 78 | trie_node[word_keeper_key] = True |
| 79 | |
| 80 | len_string = len(string) |
| 81 | |
| 82 | # Dynamic programming method |
| 83 | @functools.cache |
| 84 | def is_breakable(index: int) -> bool: |
nothing calls this directly
no test coverage detected