MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / word_break

Function word_break

backtracking/word_break.py:48–74  ·  view source on GitHub ↗

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 i

(input_string: str, word_dict: set[str])

Source from the content-addressed store, hash-verified

46
47
48def word_break(input_string: str, word_dict: set[str]) -> bool:
49 """
50 Determines if the input string can be segmented into a sequence of
51 valid dictionary words using backtracking.
52
53 Parameters:
54 input_string (str): The input string to segment.
55 word_dict (set[str]): The set of valid words.
56
57 Returns:
58 bool: True if the string can be segmented into valid words, otherwise False.
59
60 Example:
61 >>> word_break("leetcode", {"leet", "code"})
62 True
63
64 >>> word_break("applepenapple", {"apple", "pen"})
65 True
66
67 >>> word_break("catsandog", {"cats", "dog", "sand", "and", "cat"})
68 False
69
70 >>> word_break("applepenapple", {})
71 False
72 """
73
74 return backtrack(input_string, word_dict, 0)

Callers

nothing calls this directly

Calls 1

backtrackFunction · 0.70

Tested by

no test coverage detected