Given a string, generate all permutations of it that do not contain ‘B’ after ‘A’, i.e., the string should not contain “AB” as a substring.**Examples:**

Input :str = “ABC”Output :ACB, BAC, BCA, CBA

Out of 6 permutations of “ABC”, 4 follow the given constraint and 2 (“ABC” and “CAB”) do not follow.Input :str = “BCD”Output :BCD, BDC, CDB, CBD, DCB, DBC

A **simple solution** is to generate all permutations. For every permutation, check if it follows the given constraint.

## C++

`// Simple CPP program to print all permutations ` `// of a string that follow given constraint` `#include <bits/stdc++.h>` `using` `namespace` `std;` `void` `permute(string& str, ` `int` `l, ` `int` `r)` `{` ` ` `// Check if current permutation is` ` ` `// valid` ` ` `if` `(l == r) {` ` ` `if` `(str.find(` `"AB"` `) == string::npos)` ` ` `cout << str << ` `" "` `;` ` ` `return` `;` ` ` `}` ` ` `// Recursively generate all permutation` ` ` `for` `(` `int` `i = l; i <= r; i++) {` ` ` `swap(str[l], str[i]);` ` ` `permute(str, l + 1, r);` ` ` `swap(str[l], str[i]);` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"ABC"` `;` ` ` `permute(str, 0, str.length() - 1);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Simple Python3 program to print all permutations ` `# of a string that follow given constraint` `def` `permute(` `str` `, l, r):` ` ` ` ` `# Check if current permutation is` ` ` `# valid` ` ` `if` `(l ` `=` `=` `r):` ` ` `if` `"AB"` `not` `in` `''.join(` `str` `):` ` ` `print` `(''.join(` `str` `), end ` `=` `" "` `)` ` ` `return` ` ` ` ` `# Recursively generate all permutation` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `):` ` ` `str` `[l], ` `str` `[i] ` `=` `str` `[i], ` `str` `[l]` ` ` `permute(` `str` `, l ` `+` `1` `, r)` ` ` `str` `[l], ` `str` `[i] ` `=` `str` `[i], ` `str` `[l]` ` ` `# Driver Code` `str` `=` `"ABC"` `permute(` `list` `(` `str` `), ` `0` `, ` `len` `(` `str` `) ` `-` `1` `)` `# This code is contributed by SHUBHAMSINGH10` |

*chevron_right*

*filter_none*

**Output**

ACB BAC BCA CBA

The above solution first generates all permutations, then for every permutation it checks if it follows given constraint or not.

An** efficient solution **is to use Backtracking. We cut down the recursion tree whenever we see that substring “AB” is formed. How do we do this? we add a isSafe() function. Before doing a swap, we check if previous character is ‘A’ and current character is ‘B’.

## C++

`// Backtracking based CPP program to print all` `// permutations of a string that follow given` `// constraint` `#include <bits/stdc++.h>` `using` `namespace` `std;` `bool` `isSafe(string& str, ` `int` `l, ` `int` `i, ` `int` `r)` `{` ` ` `// If previous character was 'A' and character` ` ` `// is 'B', then do not proceed and cut down` ` ` `// the recursion tree.` ` ` `if` `(l != 0 && str[l - 1] == ` `'A'` `&& str[i] == ` `'B'` `)` ` ` `return` `false` `;` ` ` `// This condition is explicitly required for` ` ` `// cases when last two characters are "BA". We` ` ` `// do not want them to swapped and become "AB"` ` ` `if` `(r == l + 1 && str[i] == ` `'A'` `&& str[l] == ` `'B'` ` ` `|| r == l + 1 && l == i && str[r] == ` `'B'` ` ` `&& str[l] == ` `'A'` `)` ` ` `return` `false` `;` ` ` `return` `true` `;` `}` `void` `permute(string& str, ` `int` `l, ` `int` `r)` `{` ` ` `// We reach here only when permutation` ` ` `// is valid` ` ` `if` `(l == r) {` ` ` `cout << str << ` `" "` `;` ` ` `return` `;` ` ` `}` ` ` `// Fix all characters one by one` ` ` `for` `(` `int` `i = l; i <= r; i++) {` ` ` `// Fix str[i] only if it is a` ` ` `// valid move.` ` ` `if` `(isSafe(str, l, i, r)) {` ` ` `swap(str[l], str[i]);` ` ` `permute(str, l + 1, r);` ` ` `swap(str[l], str[i]);` ` ` `}` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"ABC"` `;` ` ` `permute(str, 0, str.length() - 1);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python

`# Backtracking based Python program to prall` `# permutations of a string that follow given` `# constraint` `def` `isSafe(` `str` `, l, i, r):` ` ` `# If previous character was 'A' and character` ` ` `# is 'B', then do not proceed and cut down` ` ` `# the recursion tree.` ` ` `if` `(l !` `=` `0` `and` `str` `[l ` `-` `1` `] ` `=` `=` `'A'` `and` `str` `[i] ` `=` `=` `'B'` `):` ` ` `return` `False` ` ` `# This condition is explicitly required for` ` ` `# cases when last two characters are "BA". We` ` ` `# do not want them to swapped and become "AB"` ` ` `if` `(r ` `=` `=` `l ` `+` `1` `and` `str` `[i] ` `=` `=` `'A'` `and` `str` `[l] ` `=` `=` `'B'` ` ` `or` `r ` `=` `=` `l ` `+` `1` `and` `l ` `=` `=` `i ` `and` `str` `[r] ` `=` `=` `'B'` ` ` `and` `str` `[l] ` `=` `=` `'A'` `):` ` ` `return` `False` ` ` `return` `True` `def` `permute(` `str` `, l, r):` ` ` `# We reach here only when permutation` ` ` `# is valid` ` ` `if` `(l ` `=` `=` `r):` ` ` `print` `(` `*` `str` `, sep` `=` `"` `", end="` `")` ` ` `return` ` ` `# Fix all characters one by one` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `):` ` ` `# Fix str[i] only if it is a` ` ` `# valid move.` ` ` `if` `(isSafe(` `str` `, l, i, r)):` ` ` `str` `[l], ` `str` `[i] ` `=` `str` `[i], ` `str` `[l]` ` ` `permute(` `str` `, l ` `+` `1` `, r)` ` ` `str` `[l], ` `str` `[i] ` `=` `str` `[i], ` `str` `[l]` `# Driver Code` `str` `=` `"ABC"` `permute(` `list` `(` `str` `), ` `0` `, ` `len` `(` `str` `) ` `-` `1` `)` `# This code is contributed by SHUBHAMSINGH10` |

*chevron_right*

*filter_none*

**Output**

ACB BAC BCA CBA

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Generate all cyclic permutations of a number
- Generate all possible permutations of a Number divisible by N
- Iterative program to generate distinct Permutations of a String
- Generate permutations with only adjacent swaps allowed
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Check if it is possible to convert one string into another with given constraints
- Construct a binary string following the given constraints
- Remove minimum characters from string to split it into three substrings under given constraints
- Find value after N operations to remove N characters of string S with given constraints
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print all the palindromic permutations of given string in alphabetic order
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Print all lexicographical greater permutations of a given string
- Generate all rotations of a given string
- Program to generate all possible valid IP addresses from given string
- Program to generate all possible valid IP addresses from given string | Set 2
- Generate a string which differs by only a single character from all given strings
- Print all palindrome permutations of a string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.