Recursion Codes of the String Problem

My code is giving correct output but still not passing a singe test case
link to my code:-https://ide.codingblocks.com/s/55738

Can u pls mention question link

ques link :- https://hack.codingblocks.com/practice/p/390/346

Wrong test case:
Input
1125

Correct output
[aabe, aay, ale, kbe, ky]
your output
C

Well i used \b to remove the extra comma at the end and its working fine in my ide

Can u share the latest code.Final Code you are submitting

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define MOD 1000000007
char ar[] = {’ ‘,‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,‘l’,‘m’,‘n’,‘o’,‘p’,‘q’,‘r’,‘s’,‘t’,‘u’,‘v’,‘w’,‘x’,‘y’,‘z’};
int n;
void solve(char *in,char *out,int i,int j){
if(in[i]==’\0’){
out[j]=’\0’;
cout<<out<<", ";
return ;
}

int digit = in[i]-'0';
out[j]=ar[digit];
solve(in,out,i+1,j+1);

if(i+1<n){
int digit2 = (in[i]-'0')*10+(in[i+1]-'0');
if(digit2<=26){
    out[j] = ar[digit2];
    solve(in, out, i+2,j+1);
    }
}

}

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);

char in[1000],out[1000];
cin>>in;

n = strlen(in);
cout<<"[";
solve(in,out,0,0);
cout<<"\b\b]";
return 0;

}

paste code on IDE and share link

https://ide.codingblocks.com/s/55757

I think the problem is in /b/b.
You can store all the string in vector < string > and at last print all the strings

Ok let me try that approach

Thanks a lot. It works correctly now.

1 Like

here is the solution to the code

#include<bits/stdc++.h>
using namespace std;

void generate_allpossstrings(vector&v,char *out,char *inp,int i,int j)
{
if(inp[i]==’\0’)
{
out[j]=’\0’;
string s;
s=out;
v.push_back(s);
//cout<<out;
//cout<<", ";
return ;
}

int digit=inp[i]-'0';
char ch=digit+'a'-1;
out[j]=ch;

if(inp[i+1]!='0' && inp[i]!='0')generate_allpossstrings(v,out,inp,i+1,j+1);

if(inp[i+1]!='\0')
{
	int seconddigit=inp[i+1]-'0';
	int no=digit*10+seconddigit;
	if(no<=26)
	{
		ch=no+'a'-1;
		out[j]=ch;
		if(inp[i]!='0')generate_allpossstrings(v,out,inp,i+2,j+1);

	}
}

}
int main() {
char inp[100];
char out[100];
vectorv;
cin>>inp;
generate_allpossstrings(v,out,inp,0,0);
cout<<"[";
auto it=v.begin();
for(;it!=v.end()-1;it++)
{
cout<<*it<<", “;
}
cout<<*it;
cout<<”]";
return 0;
}