For a string such as "helloyellowellow", parse all valid strings from the given string. (For example: [[hell, hello, yellow], [low, low] ........]
I am looking for the most optimized way to write code. Here is mine, but I'm not sure if this is the best way.
Full disclosure - This was an interview question.
master = [] # Dictionary for us to look up words def is_word(inputstr): #returns True/False def processstring(fstr,secstr,li): if is_word(fstr): li.append(fstr) if len(secstr) == 0: if len(li) != 0: master.append(li) return processstring(fstr+secstr[0], secstr[1:len(secstr)],li) def wrapperprocess(inpstr): li = [] if len(inpstr) == 0: return processstring('',inpstr,li) wrapperprocess(inpstr[1:len(inpstr)]) wrapperprocess('helloyellowellow') print master
source share