Recursion dictionary doubt

#include
#include
#include
using namespace std;
bool compare(string s1,string s2){
return s1<s2;
}
void permute(string ip,int i,string a,int &k,string arr[]){
sort (arr,arr+k);

if(ip[i]=='\0'){
	
	if(ip<a){
	arr[k]=ip;
    k++;
}
}
for(int j=i;j<ip.length();j++){
	swap(ip[i],ip[j]);
	permute(ip,i+1,a,k,arr);
	swap(ip[i],ip[j]);
}

}
int main() {
string ip;

cin>>ip;
string a;
a=ip;
string arr[1000];

int k=0;
permute(ip,0,a,k,arr);
sort(arr,arr+k,compare);
for(int i=0;i<k;i++){
cout<<arr[i]<<endl;
}

return 0;

}
why this code is not passing all the tst cases its giving me tle in some test cases

this code is for dictionary (smaller)

Hello @deepakjumani09,

Please share your code using some online IDE.

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.