Search
 
SCRIPT & CODE EXAMPLE
 

CPP

permutation in c++ with backtracking

// C++ program to print all
// permutations with duplicates allowed
#include <bits/stdc++.h>
using namespace std;
 
 
// Function to print permutations of string
// This function takes three parameters:
// 1. String
// 2. Starting index of the string
// 3. Ending index of the 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]);
        }
    }
}
 
// Driver Code
int main()
{
    string str = "ABC";
    int n = str.size();
    permute(str, 0, n-1);
    return 0;
}
 
// This is code is contributed by rathbhupendra
Comment

PREVIOUS NEXT
Code Example
Cpp :: transpose function example in c++ 
Cpp :: reading matrix from text file in c++ and adding them and then storing them in oother c++ file 
Cpp :: overloading templates in cpp 
Cpp :: default parameter c++ a field 
Cpp :: C++ Multilevel Inheritance 
Cpp :: . Single-line comments start with two forward slashes (//). 
Cpp :: c++ x y in arrau 1d 
Cpp :: c++ program that put a space in between characters 
Cpp :: stl map 
Cpp :: c++ to c code converter online 
Cpp :: entering char in int c++ avoid loop 
Cpp :: how to i convert C++ into C 
Cpp :: qt get wireless interface name 
Cpp :: c++ 2 dim array initialize 
Cpp :: solve diamond inheritance c++ 
Cpp :: time function in c++ 
Cpp :: flutter container margin 
Cpp :: matrix chainmultiplication 
Cpp :: how to change the default camera speed values opengl 
Cpp :: c++ string to vector using delimiter 
Cpp :: how to insert variable into string c++ 
Cpp :: how to show c++ binary files in sublime text 
Cpp :: 378. Kth Smallest Element in a Sorted Matrix using binary search 
Cpp :: pass address to function c++ 
Cpp :: is plaindrome 
Cpp :: minimum no of jump required to reach end of arry 
Cpp :: windows install cppcheck 
Cpp :: vprintf 
Cpp :: what is a string called in c++ 
Cpp :: assoc-right antlr 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =