Skip to content

add word_break dynamic approach up -> down. #8039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 30, 2022
Merged
105 changes: 105 additions & 0 deletions dynamic_programming/word_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""
Author : Alexander Pantyukhin
Date : December 12, 2022

Task:
Given a string s and a dictionary of strings wordDict,
return true if s can be segmented into a space-separated
sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused
multiple times in the segmentation.

Implementation notes: Trie + Dynamic programming up -> down.
The Trie keeps all wordDict words. It will be useful for scanning
available words for the current position in the string.

Leetcode:
https://leetcode.com/problems/word-break/description/

Runtime: O(n * n)
Space: O(n)
"""

from functools import lru_cache

def wordBreak(s: str, word_dict: list[str]) -> bool:
"""
Return True if numbers have opposite signs False otherwise.

>>> wordBreak("applepenapple", ["apple","pen"])
True
>>> wordBreak("catsandog", ["cats","dog","sand","and","cat"])
False
>>> wordBreak("cars", ["car","ca","rs"])
True
>>> wordBreak("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"])
False
>>> wordBreak('abc', [])
False
>>> wordBreak(123, ['a'])
Traceback (most recent call last):
...
ValueError: the s should be not empty string
>>> wordBreak('', ['a'])
Traceback (most recent call last):
...
ValueError: the s should be not empty string
>>> wordBreak('abc', [123])
Traceback (most recent call last):
...
ValueError: the word_dict should a list of non empty string
>>> wordBreak('abc', [''])
Traceback (most recent call last):
...
ValueError: the word_dict should a list of non empty string
"""

# Validation
if not isinstance(s, str) or len(s) == 0:
raise ValueError('the s should be not empty string')

if not isinstance(word_dict, list) or not all(
[isinstance(item, str) and len(item) > 0 for item in word_dict]):
raise ValueError('the word_dict should a list of non empty string')

# Build trie
trie = {}
WORD_KEEPER = 'WORD_KEEPER'
for word in word_dict:
trie_node = trie
for c in word:
if c not in trie_node:
trie_node[c] = {}

trie_node = trie_node[c]

trie_node[WORD_KEEPER] = True

len_s = len(s)

# Dynamic programming method
@lru_cache(maxsize=None)
def is_breakable(index: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/word_break.py, please provide doctest for the function is_breakable

if index == len_s:
return True

trie_node = trie
for i in range(index, len_s):
trie_node = trie_node.get(s[i], None)

if trie_node is None:
return False

if trie_node.get(WORD_KEEPER, False) and is_breakable(i + 1):
return True

return False

return is_breakable(0)


if __name__ == "__main__":
import doctest

doctest.testmod()