From 950f4c5e667cdd001a99015dc8c5542f1a0d5581 Mon Sep 17 00:00:00 2001 From: Aswin P Kumar Date: Thu, 3 Oct 2024 01:09:03 +0530 Subject: [PATCH 1/3] Add Word Break algorithm --- backtracking/word_break.py | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 backtracking/word_break.py diff --git a/backtracking/word_break.py b/backtracking/word_break.py new file mode 100644 index 000000000000..8496155bfc43 --- /dev/null +++ b/backtracking/word_break.py @@ -0,0 +1,69 @@ +""" +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(s: 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: + s (str): The input string. + 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(s): + return True + + # Try every possible substring from 'start' to 'end' + for end in range(start + 1, len(s) + 1): + if s[start:end] in word_dict and backtrack(s, word_dict, end): + return True + + return False + + +def word_break(s: str, word_dict: set[str]) -> bool: + """ + Determines if the input string can be segmented into a sequence of + one or more dictionary words using backtracking. + + Parameters: + s (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(s, word_dict, 0) From 332c6fe12bed73054d82efcc289aae22aca3f5dc Mon Sep 17 00:00:00 2001 From: Aswin P Kumar Date: Thu, 3 Oct 2024 01:17:43 +0530 Subject: [PATCH 2/3] Add Word Break algorithm --- backtracking/word_break.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backtracking/word_break.py b/backtracking/word_break.py index 8496155bfc43..b9bbc822a76e 100644 --- a/backtracking/word_break.py +++ b/backtracking/word_break.py @@ -7,13 +7,13 @@ """ -def backtrack(s: str, word_dict: set[str], start: int) -> bool: +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: - s (str): The input string. + 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. @@ -32,24 +32,24 @@ def backtrack(s: str, word_dict: set[str], start: int) -> bool: """ # Base case: if the starting index has reached the end of the string - if start == len(s): + if start == len(input_string): return True # Try every possible substring from 'start' to 'end' - for end in range(start + 1, len(s) + 1): - if s[start:end] in word_dict and backtrack(s, word_dict, 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(s: str, word_dict: set[str]) -> bool: +def word_break(input_string: str, word_dict: set[str]) -> bool: """ Determines if the input string can be segmented into a sequence of - one or more dictionary words using backtracking. + valid dictionary words using backtracking. Parameters: - s (str): The input string to segment. + input_string (str): The input string to segment. word_dict (set[str]): The set of valid words. Returns: @@ -66,4 +66,4 @@ def word_break(s: str, word_dict: set[str]) -> bool: False """ - return backtrack(s, word_dict, 0) + return backtrack(input_string, word_dict, 0) From 848306146157679a376307cd4c6144b882a25171 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 19:48:26 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/word_break.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backtracking/word_break.py b/backtracking/word_break.py index b9bbc822a76e..1f2ab073f499 100644 --- a/backtracking/word_break.py +++ b/backtracking/word_break.py @@ -37,7 +37,9 @@ def backtrack(input_string: str, word_dict: set[str], start: int) -> bool: # 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): + if input_string[start:end] in word_dict and backtrack( + input_string, word_dict, end + ): return True return False