what will be recursive approach to solve the question?
Mapped String Java
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.