can you please suggest the approach for the problem
Smart keypad 1 problem
this problem can be approached using recursive formulation.
say x is our string input and f(x) is our solution. recursive formulation would be:
f(x) = (character set of code of x[0] ) * f(x[1:]), here * is cross product and x[1:] is string x after removing first character.
eg. f(“231”) = (character set of code of ‘2’) * f(“31”)
= (a,b,c) * f(“31”)
this is recursive function as f is written in terms of f.
Now write algorithm for this:
prototype of function, say function name as encode:
public void encode(string x, string res, ArrayList ans){
1.write base condition here //think about base condition
if(base_condition){
ans.add(res);
return ;
}
2. char_code_array = get character array of code of x.charAt(0) // i.e ‘2’ to [a,b,c]
3. for(char c in char_code_array){
encode(x.substring(1), res+c, ans)
}
}
call encode function and ans will contain your final solution.
Thanks.
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.