Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Word Break algorithm (TheAlgorithms#11687)
* Add Word Break algorithm

* Add Word Break algorithm

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AswinPKumar01 and pre-commit-ci[bot] authored Oct 3, 2024
commit 080e7903a06765808c12c0c9c0b242f485cb9ce7
71 changes: 71 additions & 0 deletions backtracking/word_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
Word Break Problem is a well-known problem in computer science.
Given a string and a dictionary of words, the task is to determine if
the string can be segmented into a sequence of one or more dictionary words.

Wikipedia: https://en.wikipedia.org/wiki/Word_break_problem
"""


def backtrack(input_string: str, word_dict: set[str], start: int) -> bool:
"""
Helper function that uses backtracking to determine if a valid
word segmentation is possible starting from index 'start'.

Parameters:
input_string (str): The input string to be segmented.
word_dict (set[str]): A set of valid dictionary words.
start (int): The starting index of the substring to be checked.

Returns:
bool: True if a valid segmentation is possible, otherwise False.

Example:
>>> backtrack("leetcode", {"leet", "code"}, 0)
True

>>> backtrack("applepenapple", {"apple", "pen"}, 0)
True

>>> backtrack("catsandog", {"cats", "dog", "sand", "and", "cat"}, 0)
False
"""

# Base case: if the starting index has reached the end of the string
if start == len(input_string):
return True

# Try every possible substring from 'start' to 'end'
for end in range(start + 1, len(input_string) + 1):
if input_string[start:end] in word_dict and backtrack(
input_string, word_dict, end
):
return True

return False


def word_break(input_string: str, word_dict: set[str]) -> bool:
"""
Determines if the input string can be segmented into a sequence of
valid dictionary words using backtracking.

Parameters:
input_string (str): The input string to segment.
word_dict (set[str]): The set of valid words.

Returns:
bool: True if the string can be segmented into valid words, otherwise False.

Example:
>>> word_break("leetcode", {"leet", "code"})
True

>>> word_break("applepenapple", {"apple", "pen"})
True

>>> word_break("catsandog", {"cats", "dog", "sand", "and", "cat"})
False
"""

return backtrack(input_string, word_dict, 0)