This is the link to the problem CodeChef: Practical coding for everyone
I tried to solve it using inbuilt function next_permutation.
I can’t actually figure out why it is giving WA with my code.
My code is:
#include <bits/stdc++.h>
using namespace std;
bool admitted(string str)
{
int len = str.size();
for(int i=1; i<len; i++)
{
if(str[i] == str[i-1])
return false;
}
return true;
}
void nPermute(string str, long int n)
{
string prev;
sort(str.begin(), str.end());
int i = 1;
do
{
//cout<<str<<" "<<i<<endl;
if (i == n+1)
break;
if(admitted(str) == true)
{
i++;
prev = str;
}
} while (next_permutation(str.begin(), str.end()));
if(i == n+1)
cout << prev << endl;
else
cout << "-1" << endl;
}
// Driver code
int main()
{
string str;
cin>>str;
int roll;
cin>>roll;
nPermute(str, roll);
return 0;
}