String/All permutation of a string.cpp
// Function to find all permutations of a given string
// containing all distinct characters
Function to find all permutations of a given string
containing all distinct characters
Function to find all permutations of a given string containing all distinct characters
void permutations(string str, int i, int n)
{
    if (i == n - 1)
    {
        cout << str << endl;
        return;
    }
    for (int j = i; j < n; j++)
    {
        swap(str[i], str[j]);        
        permutations(str, i + 1, n);
        swap(str[i], str[j]);
    }
}
void permutations ( string str , int i , int n ) { if ( i == n - 1 ) { cout << str << endl ; return ; } for ( int j = i ; j < n ; j ++ ) { swap ( str [ i ] , str [ j ] ) ; permutations ( str , i + 1 , n ) ; swap ( str [ i ] , str [ j ] ) ; } }
Function to find all permutations of a given string
containing all distinct characters
Function to find all permutations of a given string containing all distinct characters
// base condition // process each character of the remaining string // swap character at index `i` with the current character // STL `swap()` used // recur for substring `str[i+1, n-1]` // backtrack (restore the string to its original state)