We need to write a program that implements the word break problem. The problem: given a string s and a dictionary (list of words), determine if s can be segmented into a sequence of dictionary words. Possibly also return the possible segmentations.
But the prompt says: "Write a program that implements the word break problem." It's ambiguous. It might be asking to write a solution in a specific language? Not specified. Usually these challenges expect code in Python, Java, C++, etc. Since no language specified, we can provide a solution in Python (common). Also, we should include explanation.
But the instruction is in a chat: "Write a program that implements the word break problem." Possibly we need to output the code only? But given typical ChatGPT interactions, we should provide a solution with explanation and code. However, as an AI, I need to produce a final answer. The context: "You are an AI programming assistant." So we need to write a program. The answer should include code. But also might need to describe approach.
Better to provide a clear solution: define function word_break(s, wordDict) returns boolean (or list of sentences). Usually the problem is to return True/False. But "word break problem" can refer to both. I'll assume the classic LeetCode problem: given a string s and a dictionary of words, return true if s can be segmented into a space-separated sequence of dictionary words. We can also return all possible sentences.
But let's read the prompt: "Write a program that implements the word break problem." It doesn't specify input/output format. So we can implement a function that takes string and list, and prints whether it can be segmented or all possible segmentations.
Better to implement both? Simpler: return boolean.
I'll write a Python program using dynamic programming.
We'll have:
def word_break(s, wordDict):
n = len(s)
dp = [False]*(n+1)
dp[0] = True
word_set = set(wordDict)
for i in range(1, n+1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break
return dp[n]
But also can be optimized with early break. That's fine.
If we need to return all possible sentences, we can use backtracking with memoization.
Given the open-endedness, I'll produce a program that both checks and prints all possible segmentations if needed. But the prompt says "implements the word break problem", likely just the core algorithm. I'll include an example usage.
Thus, final answer: Provide code in Python.
But the conversation: The user said "Write a program that implements the word break problem." That's all. We should respond with a program. We can include explanation, but the main output is the code.
As per instruction "You are an AI programming assistant. You will be provided with a task, and your goal is to complete it as accurately as possible." So we need to output the program.
I think we should output a well-commented code snippet, maybe with a main function that demonstrates.
Let's produce:
python
def word_break(s, word_dict):
"""
Returns True if s can be segmented into a sequence of words from word_dict.
Uses dynamic programming.
"""
word_set = set(word_dict)
n = len(s)
dp = [False] * (n + 1)
dp[0] = True # empty string can be segmented
for i in range(1, n + 1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break
return dp[n]
if __name__ == "__main__":
# Example usage
s = "leetcode"
word_dict = ["leet", "code"]
print(word_break(s, word_dict)) # True
s2 = "applepenapple"
word_dict2 = ["apple", "pen"]
print(word_break(s2, word_dict2)) # True
s3 = "catsandog"
word_dict3 = ["cats", "dog", "sand", "and", "cat"]
print(word_break(s3, word_dict3)) # False
That's a simple program.
But maybe they want the problem that returns all possible sentences. Many references: "Word Break" is LeetCode 139 (boolean) and "Word Break II" is 140 (all sentences). The phrase "word break problem" could refer to either, but typically the problem is to determine if it's possible. I'll go with boolean.
Alternatively, to be safe, I can mention both and provide code for both. But the instruction is to write a program, so one program is fine.
Given the simplicity, I'll output the code.
But also, maybe they want a complete runnable program that reads input? Not specified. Usually we write functions.
Thus, final answer: Provide code in Python.
I'll include explanation in the response? The assistant should output the program. I can also include a brief explanation before code. However, the prompt might expect only code. In many coding tasks, they want the solution code. I can provide both explanation and code, but it's okay.
Given the context, I'll produce a final answer with explanation and then code.
I'll write:
The word break problem can be solved using dynamic programming. Here's a Python implementation that returns True if the string can be segmented, else False.
Then code.
Make sure it's clear.
Thus, final.
python
def word_break(s: str, word_dict: list) -> bool:
"""
Returns True if the string s can be segmented into a sequence of words
from word_dict, otherwise False.
Uses dynamic programming: dp[i] is True if s[0:i] can be segmented.
"""
word_set = set(word_dict) # O(1) lookup
n = len(s)
dp = [False] * (n + 1)
dp[0] = True # empty string is always valid
for i in range(1, n + 1):
for j in range(i):
# If prefix up to j is valid and substring s[j:i] is a word
if dp[j] and s[j:i] in word_set:
dp[i] = True
break # no need to check other splits
return dp[n]
if __name__ == "__main__":
# Example test cases
test_cases = [
("leetcode", ["leet", "code"], True),
("applepenapple", ["apple", "pen"], True),
("catsandog", ["cats", "dog", "sand", "and", "cat"], False)
]
for s, words, expected in test_cases:
result = word_break(s, words)
print(f"word_break({s!r}, {words!r}) -> {result} (expected {expected})")