Wednesday, May 18, 2016

[BackTracking] [C++] Permutations of given string


// STRING PERMUTATIONS
void swap(char &a, char &b)
{
a = a^b;
b = a^b;
a = a^b;
}
void _strPermute(char *s, int n, int pos)
{
if (pos == n-1)
{
cout << s << endl;
return;
}
for (int i = pos + 1; i < n; i++)
{
swap(s[pos], s[i]);
_strPermute(s, n, pos + 1);
swap(s[pos], s[i]);
}
}
void printStringPermutations(char *s, int n)
{
if (n < 2) return;
_strPermute(s, n, 0);
}

No comments:

Post a Comment

GraphQL

GraphQL What is GraphQL It is a specification laid out by Facebook which proposed an alternative way to query and modify data. Think o...