I'm trying to solve a leetcode problem called word break https://leetcode.com/problems/word-break/
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
I'm able to print the different word breaks that fit the solution but when returning my code always returns None. How can I fix it so that res is an array containing the different words in the dictionary that create s
import sys;
class Solution:
def wordBreak(self, s, wordDict):
res = self.driver(s, 0, len(s), wordDict, [])
print(res)
def driver(self, text, start, end, wordDict, res):
if text[start:end] == None:
return res
elif text[start:end] in wordDict:
result = text[start:end]
res.append(result)
print(res)
return self.driver(text, end, len(text), wordDict, res)
else:
for i in range(start, end):
self.driver(text, start, i, wordDict, res)
driverdoes not return anything in the recursive case.Solutionfor their code challenges