Just a little recursion issue in Java

The first thing that makes me dubious about your code is that you are supposed to returning a series of strings, but your return value is a string.

Perhaps, you should nail down your base case and recursive step.

It looks like you’ve got a start on the base case. You can insert zero spaces in the empty string, so

allPossibleSpacings("") -> [ "" ]

but you don’t want to insert a space at the end, so you need a second base case

allPossibleSpacings("x") -> [ "x" ]

and then your recursive step could be

allPossibleSpacings("x" + s) -> flatten(
    ∀ t : allPossibleSpacings(s), ["x" + t, "x " + t])

I won’t help you write that in Java since it’s homework, but hope that helps.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)