The TAs explain way better than this guy. Can you please explain the approach to this problem
Codes of the string
@aiman.mumtaz
see here you have to make all the possible string by mapping the digits to the chars.
Taking the sample input:- 123
ABC using 1-A,2-B,3-C separately
AW using 1-A, (23)-W
LC using (12)-L, 3-C
I hope you got how the above answers are calculated.
Now moving on how recursive code will work.
Here we need to do only two things.
- Take the single digit and convert it into mapped characters.
- Check it is possible to combine that single digit with next digit.
For eg:- if current digit is 2 and next is 5, then we can do option 2.
but if they are 2 and 7, then option 2 is not possible.
So my code is totally based upon these 2 things.
void solve(string s, int i, int n, string curr){
if(i==n){
cout<<curr<<endl;
return;
}
else{
int digit = s[i]-'0';
solve(s, i+1, n, curr + key[digit]); //calling recursion because step 1 is done
if(n-i>=2){ //check if i is n-1 then there is no next digit
int digit1 = s[i]-'0';
int digit2 = s[i+1]-'0';
int num = digit1*10 + digit2;
if(num<=26 && num>9) //now checking if the number formed is satisfying step 2
solve(s, i+2, n, curr+key[num]); //if yes then call recursion and change i to i+2
}
}
}
I hope you have understood it now.
Let me know if there is some doubt left.
If it is helpful then please mark this doubt as resolved.
Yes. I got the logic and implemented the code. I need one tiny help. How to remove the comma from the last permutation??
@aiman.mumtaz
make a global count variable, print ‘,’ just before printing the cout<<out , with the help of global count variable don’t print ‘,’ for count=0.
this is the code
@aiman.mumtaz
please also remember to mark this doubt as resolved if it was helpful and able to clear your doubt.