Code giving wrong output

Please suggest me changes to make in the code to get the correct answer
#include <bits/stdc++.h>
using namespace std;
unordered_map<string,string> mp={
{“1”,“a”},
{“2”,“b”},
{“3”,“c”},
{“4”,“d”},
{“5”,“e”},
{“6”,“f”},
{“7”,“g”},
{“8”,“h”},
{“9”,“i”},
{“10”,“j”},
{“11”,“k”},
{“12”,“l”},
{“13”,“m”},
{“14”,“n”},
{“15”,“o”},
{“16”,“p”},
{“17”,“q”},
{“18”,“r”},
{“19”,“s”},
{“20”,“t”},
{“21”,“u”},
{“22”,“v”},
{“23”,“w”},
{“24”,“x”},
{“25”,“y”},
{“26”,“z”}
};

void print(string s,int i,string out)
{
if(s[i]==’\0’)
{
cout<<out<<i<<endl;
return;
}

out+=mp[s.substr(i,1)];
print(s,i+1,out);

string temp=s.substr(i,2);
if(stoi(temp)<=26 and s.length()-i>=2)
{

out=out+mp[s.substr(i,2)];
print(s,i+2,out);

}

}

int main()
{
string in=“1125”;
string out;

print(in,0,out);

return 0; 

}

please copy the code in CB ide it becomes difficult to interpret this way

Since u are updating the out string

u need to remove the last inserted value when u are moving to the next recursion call

simply add a statement out.pop_back() after the first recursive call
before string temp is being declared

1 Like

Thanks a ton.
Understood my mistake.

1 Like

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.