Mapped String Java

what will be recursive approach to solve the question?

@Anubhav44044,

Assume the recursion works and will give you answer after the first digit.

Recursive action : All you need to do is work for the 0th index element. Because it could have a part included in the answer or not.

So first number could be mapped as it is or can be mapped by including just the next digit with.

For e.g…, 123 either first digit will be mapped as β€˜1’ or as β€˜12’ but not for β€˜123’

First we need to do the recursive formulation of the problem.
lets say original input is x and f(x) is solution of the problem. then we can write f(x) recursively as:
f(x) = {code of first digit + f(x[1:]),
code of first 2 digits + f(x[2:]) if first 2 digits<=26
}

here x[r:] means remaining part of x leaving first r digits.

lets see one example of this formulation:
say x = 123
step1: f(123) ={ code of 1 + f(23), code of 12 + f(3) }
= {β€œA” + f(23), β€œL” + f(3)}
step2: f(23) = {code of 2 + f(3), code of 23 + f(nothing)}
= {β€œB” + f(3) , β€œW” + f(nothing)}
step3: f(3) = {code of 3 + f(nothing)} = {β€œC” + f(nothing)} = {β€œC”}
step4: f(nothing) = β€œβ€ …this is your base condition of recursion.

put all computed things in step1:
f(123) = {β€œA” + {β€œBC” , β€œW”}, β€œL”+β€œC”} = {β€œABC” , β€œAW”, β€œLC”}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.