Generate all Permutations of a String that follow given Constraints


Introduction

In this tutorial, we implement two examples using C++ programming concepts to generate all permutations of an input string. Permutation of a string is the number of ways a string can be arranged by interchanging the position of characters. We also include some constraints or limitations. All permutations or arrangements of the input string ensure character B does not follow character A anywhere, meaning there is no AB combination in the string.

To implement this task we use two approaches:

  • Directly generate all combinations of the string while restricting AB.

  • Using backtracking.

Demonstration 1

String = “ACB”

Output

BAC, CBA, BCA

In the above demonstration, using the input string the limitation is that no combination of AB appears in the string permutation. The possible number of permutations are BAC, CBA, and BCA.

Demonstration 2

String = “BDC”

Output

BCD, DCB, DBC, BDC, CBD, CDB

In the above demonstration, there is no A in the input string. The string can generate all possible permutations and those arrangements are BCD, DCB, DBC, BDC, CBD, and CDB.

Demonstration 3

String = “ABD”

Output

ADB, DBA, BDA, BAD

In the above demonstration, the input string is “ABD” and the constraint is B does not follow A in any string permutations. Considering this constraint, the possible number of generated input string permutations is ADB, DBA, BDA, and BAD.

Algorithm

  • Take an input string.

  • Use the library function permute() to generate all possible permutations.

  • Use constraints with the find() function.

  • Recursively generate all permutations with constraints using a for loop.

  • Print all generated permutations of the input string.

Syntax

find() : This is a string class library function defined in the string header file. It helps search for a particular element in the input string. It returns the first index value of the searched element.

string_name.find(value);

permute() : This C++ library function generates possible permutations of the string. It takes two parameters defining the starting and ending of different string combinations.

 string_name.permute(first, end);

swap() : It is a standard library function defined in the <std> header file. It swaps two values.

 swap(varaible_1, varaible_2);  

Example 1

We implement an example with the input string “ACB”. The string npos is applied to generate all possible permutations till the end, using recursion. Use the find() function to remove AB combinations from the string.In string npos, the string values are printed to the end.

#include <bits/stdc++.h>
using namespace std;
 
void permute(string& s, int m, int a)
{
 
  //checking validity of current permutation
    if (m == a) 
    {
        if (s.find("AB") == string::npos)
            cout << s << " ";
        return;
    }
 
    // generating all possible permutation
    for (int x = m; x <= a; x++)
    {
        swap(s[m], s[x]);
        permute(s, m + 1, a);
        swap(s[m], s[x]);
    }
}
 
int main()
{
    string s = "ACB";
    permute(s, 0, s.length() - 1);
    return 0;
}

Output

ACB CBA BCA BAC 

Example 2

To implement the example, we use backtracking. In the above example implementation all permutations are generated and then the function removes the constraint. It is time-consuming and by using a backtracking approach we did not produce all permutations but generated permutations while considering removing AB

#include <bits/stdc++.h>
using namespace std;
 
bool permuteString(string& s, int m, int j, int a)
{
//removing A and B from recursion
    if (m != 0 && s[m - 1] == 'A' && s[j] == 'B')
        return false;
 
    // Remove AB combination or swapping with BA
    if (a == m + 1 && s[j] == 'A' && s[m] == 'B'
        || a == m + 1 && m == j && s[a] == 'B'
               && s[m] == 'A')
        return false;
 
    return true;
}
 
void permute(string& s, int m, int a)
{
     if (m == a) 
    {
        cout << s << " ";
        return;
    }

     for (int x = m; x <= a; x++)
    {
 
        if (permuteString(s, m, x, a)) 
        {
            swap(s[m], s[x]);
            permute(s, m + 1, a);
            swap(s[m], s[x]);
        }
    }
}
 
//Controller
int main()
{
    string s = "ACB";
   
    // calling function
    permute(s, 0, s.length() - 1);
    return 0;
}

Output

ACB CBA BCA BAC 

Conclusion

We reached the end of this tutorial to generate all possible permutations while considering a constraint that AB does not appear together. Implemented 2 examples with different approaches to resolving the task and described it using 3 demonstrations.

Using a recursive approach, we generated all possible permutations and then removed the AB combination from the result. In another example, backtracking was used which does not generate all possible combinations at once.

Updated on: 18-Aug-2023

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements