Answers for "find permutations"

C++
6

find all permutations of a string

void permute(string a, int l, int r)  
{  
    // Base case  
    if (l == r)  
        cout<<a<<endl;  
    else
    {  
        // Permutations made  
        for (int i = l; i <= r; i++)  
        {  
  
            // Swapping done  
            swap(a[l], a[i]);  
  
            // Recursion called  
            permute(a, l+1, r);  
  
            //backtrack  
            swap(a[l], a[i]);  
        }  
    }  
}
Posted by: Guest on May-01-2020
0

find permutations

void find_permutations(vector<int> &array, size_t index, vector<int> current_perm, vector<vector<int>> &res){
    if(index == array.size()) 
        res.push_back(current_perm);
    else{
        for(size_t i = 0; i <= current_perm.size(); ++i){
            vector<int> new_perm(current_perm.begin(), current_perm.end());
            new_perm.insert(new_perm.begin()+i, array[index]);
            find_permutations(array, index+1, new_perm, res);
        }
    }
}
Posted by: Guest on August-05-2021
0

find all permutations of a string

ABC
ACB
BAC
BCA
CBA
CAB
Posted by: Guest on March-30-2021

Browse Popular Code Answers by Language