Last active
May 22, 2021 13:55
-
-
Save vmarois/9f844246d9ed95b0738a29f97f2d28f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def word_break(s: str, word_dict: List[str]) -> List[str]: | |
word_dict, sentences, n = set(word_dict), [], len(s) | |
def backtrack(start: int, cur: List[str]) -> None: | |
if start == n: # solution valide, on la retient | |
sentences.append(' '.join(cur)) | |
return | |
for i in range(start + 1, n + 1): | |
if s[start: i] in word_dict: | |
# cette solution respecte les contraintes, | |
# on peut continuer à explorer les solutions suivantes | |
backtrack(i, cur + [s[start: i]]) | |
backtrack(0, []) | |
return sentences |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment