-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-break-ii.py
35 lines (30 loc) · 1.2 KB
/
word-break-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from typing import List
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
# Initialize the result list and convert wordDict to a set for O(1) lookups
res = []
wordDict = set(wordDict)
def backtrack(i, j, array):
# Base case: If we have reached the end of the string
if j == len(s) - 1:
# Check if the current substring is in the wordDict
if s[i:j + 1] in wordDict:
array.append(s[i:j + 1])
res.append(" ".join(array))
array.pop()
return
# If the current substring is in the wordDict, proceed with backtracking
if s[i:j + 1] in wordDict:
array.append(s[i:j + 1])
backtrack(j + 1, j + 1, array)
array.pop()
backtrack(i, j + 1, array)
else:
backtrack(i, j + 1, array)
# Start the backtracking process
backtrack(0, 0, [])
return res
# Example usage:
# sol = Solution()
# print(sol.wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"]))
# Output: ["cats and dog", "cat sand dog"]