Given a string and a word dict, find all possible sentences I have been trying to figure out how to meet space and time complexity requirements for the following leet code question. I have come up with two versions, one that meets the time requirements and another that meets the space requirements. Both versions fail on the same test case (the last test case shown below). Can anyone suggest how to reconcile both space and time requirements? What am I missing?
Question and examples

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
add spaces in s to construct a sentence where each word is a valid dictionary word. Return
all such possible sentences.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input:
s = &quot;catsanddog&quot;
wordDict = [&quot;cat&quot;, &quot;cats&quot;, &quot;and&quot;, &quot;sand&quot;, &quot;dog&quot;]

Output:
[
  &quot;cats and dog&quot;,
  &quot;cat sand dog&quot;
]

Example 2:
Input:
s = &quot;pineapplepenapple&quot;
wordDict = [&quot;apple&quot;, &quot;pen&quot;, &quot;applepen&quot;, &quot;pine&quot;, &quot;pineapple&quot;]

Output:
[
  &quot;pine apple pen apple&quot;,
  &quot;pineapple pen apple&quot;,
  &quot;pine applepen apple&quot;
]

Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input:
s = &quot;catsandog&quot;
wordDict = [&quot;cats&quot;, &quot;dog&quot;, &quot;sand&quot;, &quot;and&quot;, &quot;cat&quot;]

Output:
[]


Code that fails time limit
class Solution:
    def wordBreak(self, s: str, wordDict: 'List[str]') -&gt; 'List[str]':
        if not wordDict:
            return []

        wD = set(wordDict)
        max_offset = len(max(wD, key=len))
        paths = []

        def dfs(st, path):
            if st == len(s):
                paths.append(path)
                return

            for en in range(st + 1, st + max_offset + 1):
                if s[st:en] in wD:
                    dfs(en, path + [(st, en)])

        dfs(0, [])
        return [' '.join([s[x[0] : x[1]] for x in path]) for path in paths]

Code that fails space limit
class Solution:
    def wordBreak(self, s: str, wordDict: 'List[str]') -&gt; 'List[str]':
        from collections import defaultdict

        if not wordDict:
            return []

        wD = set(wordDict)
        memo = defaultdict(list)
        memo[len(s)] = [[(len(s), len(s) + 1)]]

        for i in range(len(s) - 1, -1, -1):
            for j in range(i + 1, len(s) + 1):
                if s[i:j] in wD and j in memo:
                    for l in memo[j]:
                        memo[i].append([(i, j), *l])
        return [' '.join([s[ind[0] : ind[1]] for ind in x[:-1]]) for x in memo[0]]

Testing (the last test case fails each of the above versions)
import pprint

inps = [
    (&quot;catsanddog&quot;, [&quot;cat&quot;, &quot;cats&quot;, &quot;and&quot;, &quot;sand&quot;, &quot;dog&quot;]),
    (&quot;pineapplepenapple&quot;, [&quot;apple&quot;, &quot;pen&quot;, &quot;applepen&quot;, &quot;pine&quot;, &quot;pineapple&quot;]),
    (&quot;catsandog&quot;, [&quot;cats&quot;, &quot;dog&quot;, &quot;sand&quot;, &quot;and&quot;, &quot;cat&quot;]),
    (&quot;a&quot;, ['a']),
    (&quot;abc&quot;, ['abc', 'a', 'b', 'c']),
    (&quot;hellow&quot;, []),
    (
       &quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&quot;,
       [
           &quot;a&quot;,
           &quot;aa&quot;,
           &quot;aaa&quot;,
           &quot;aaaa&quot;,
           &quot;aaaaa&quot;,
           &quot;aaaaaa&quot;,
           &quot;aaaaaaa&quot;,
           &quot;aaaaaaaa&quot;,
           &quot;aaaaaaaaa&quot;,
           &quot;aaaaaaaaaa&quot;,
       ],
    ),
]

sol = Solution()
for s, wd in inps:
    print(f&quot;Doing s[{s}] and wd[{wd}]...&quot;)
    ans = sol.wordBreak(s, wd)
    pprint.pprint(ans)
I have figured out how to do this problem and achieve the time and space requirements. It is faster than 64% of submissions but only more space efficient than 5% of submissions (woops). I ended up modifying the code that fails the space requirements. Here is my solution:

class Solution:
    def wordBreak(self, s: str, wordDict: 'List[str]') -&gt; 'List[str]':
        from collections import defaultdict

        if not wordDict:
            return []

        wD = set(wordDict)
        childs = defaultdict(list)
        childs[len(s)] = [None]

        for i in range(len(s) - 1, -1, -1):
            for j in range(i + 1, len(s) + 1):
                if s[i:j] in wD and j in childs:
                    childs[i].append(j)

        paths = []

        def dfs(nd, path):
            if nd is None:
                paths.append(path)
                return

            for ch in childs[nd]:
                dfs(ch, ' '.join([path, s[nd:ch]]))

        dfs(0, '')
        return [x.strip() for x in paths]


Any optimizations are still welcome.