Generate all permutations of a string that follow given constraints
Last Updated :
15 Apr, 2023
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 C++ 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;
}
Java
// Simple Java program to print all
// permutations of a String that
// follow given constraint
import java.util.*;
class GFG{
static void permute(char[] str, int l, int r)
{
// Check if current permutation is
// valid
if (l == r)
{
if (!String.valueOf(str).contains("AB"))
System.out.print(String.valueOf(str) + " ");
return;
}
// Recursively generate all permutation
for(int i = l; i <= r; i++)
{
char tmp = str[l];
str[l] = str[i];
str[i] = tmp;
permute(str, l + 1, r);
tmp = str[l];
str[l] = str[i];
str[i] = tmp;
}
}
// Driver Code
public static void main(String[] args)
{
String str = "ABC";
permute(str.toCharArray(), 0,
str.length() - 1);
}
}
// This code is contributed by Amit Katiyar
Python
# Simple Python 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
C#
// Simple C# program to print all permutations
// of a string that follow given constraint
using System;
using System.Text;
class GFG {
static void permute(StringBuilder str, int l, int r)
{
// Check if current permutation is
// valid
if (l == r) {
if (str.ToString().IndexOf("AB") == -1) {
Console.Write(str.ToString() + " ");
}
return;
}
// Recursively generate all permutation
for (int i = l; i <= r; i++) {
char tmp = str[l];
str[l] = str[i];
str[i] = tmp;
permute(str, l + 1, r);
tmp = str[l];
str[l] = str[i];
str[i] = tmp;
}
}
// Driver code
static void Main(string[] arg)
{
string str = "ABC";
StringBuilder s = new StringBuilder(str);
permute(s, 0, str.Length - 1);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript code to implement the above approach
function permute(str, l, r)
{
// Check if current permutation is
// valid
if (l == r) {
if (str.indexOf("AB") == -1)
document.write(str," ");
return;
}
// Recursively generate all permutation
for (let i = l; i <= r; i++) {
let a = str.split("");
let temp = a[l];
a[l] = a[i];
a[i] = temp;
permute(a.join(""), l + 1, r);
temp = a[l];
a[l] = a[i];
a[i] = temp;
}
}
// Driver Code
let str = "ABC";
permute(str, 0, str.length - 1);
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(n! X n) where n! is the number of permutations generated and O(n) times to print a permutation.
Auxiliary Space: O(n! X n )
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'.
Below is the implementation of the above code:
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";
// Function call
permute(str, 0, str.length() - 1);
return 0;
}
Java
// Backtracking based JAVA program
// to print all permutations of a
// string that follow given constraint
public class GFG
{
public boolean 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.charAt(l - 1) == 'A'
&& str.charAt(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.charAt(i) == 'A'
&& str.charAt(l) == 'B'
|| r == l + 1 && l == i
&& str.charAt(r) == 'B'
&& str.charAt(l) == 'A')
return false;
return true;
}
/**
* permutation function
* @param str string to calculate
permutation for
* @param l starting index
* @param r end index
*/
private void permute(String str,
int l, int r)
{
// We reach here only when permutation
// is valid
if (l == r)
System.out.print(str + " ");
else
{
// 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))
{
str = swap(str, l, i);
permute(str, l + 1, r);
str = swap(str, l, i);
}
}
}
}
/**
* Swap Characters at position
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
public String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
// Driver Code
public static void main(String[] args)
{
String str = "ABC";
int n = str.length();
GFG permutation = new GFG();
// Function call
permutation.permute(str, 0, n - 1);
}
}
Python
# Backtracking based Python3 program to print all
# 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"
# Function call
permute(list(str), 0, len(str) - 1)
# This code is contributed by SHUBHAMSINGH10
C#
// Backtracking based C# program to print all
// permutations of a string that follow given
// constraint
using System;
public class GFG
{
// Backtracking based C# program
// to print all permutations of a
// string that follow given constraint
using System;
using System.Text;
class GFG {
static bool isSafe(StringBuilder 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;
}
static void permute(StringBuilder str,
int l, int r)
{
// We reach here only when permutation
// is valid
if (l == r)
{
Console.Write(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))
{
char temp = str[l];
str[l] = str[i];
str[i] = temp;
permute(str, l + 1, r);
temp = str[l];
str[l] = str[i];
str[i] = temp;
}
}
}
// Driver code
static void Main()
{
string str = "ABC";
StringBuilder s = new StringBuilder(str);
// Function call
permute(s, 0, str.Length - 1);
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
const GFG = {
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 && 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;
},
permute(str, l, r) {
// We reach here only when permutation
// is valid
if (l === r) console.log(`${str} `);
else {
// Fix all characters one by one
for (let i = l; i <= r; i++) {
// Fix str[i] only if it is a
// valid move.
if (this.isSafe(str, l, i, r)) {
str = this.swap(str, l, i);
this.permute(str, l + 1, r);
str = this.swap(str, l, i);
}
}
}
},
swap(a, i, j) {
let temp;
const charArray = a.split('');
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return charArray.join('');
},
};
// Driver Code
const str = 'ABC';
const n = str.length;
// Function call
GFG.permute(str, 0, n - 1);
Similar Reads
All permutations of a string using iteration
A permutation, also called an âarrangement numberâ or âorderâ, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation ( Source: Mathword ) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB We hav
4 min read
Generate all permutation of a set in Python
Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by n!. For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {
2 min read
Distinct Numbers obtained by generating all permutations of a Binary String
Given a binary string S, the task is to print all distinct decimal numbers that can be obtained by generating all permutations of the binary string. Examples: Input: S = "110"Output: {3, 5, 6}Explanation: All possible permutations are {"110", "101", "110", "101", "011", "011"}.Equivalent decimal num
8 min read
Generate a String from given Strings P and Q based on the given conditions
Given two strings P and Q, the task is to generate a string S satisfying the following conditions: Find S such that P is rearranged and Q is a substring in it.All the characters before Q in S should be smaller than or equal to the first character in Q and in lexicographic order.The rest of the chara
7 min read
Lexicographically n-th permutation of a string
Given a string of length m containing lowercase alphabets only. You have to find the n-th permutation of string lexicographically. Examples: Input : str[] = "abc", n = 3 Output : Result = "bac" Explanation : All possible permutation in sorted order: abc, acb, bac, bca, cab, cba Input : str[] = "aba"
6 min read
Time complexity of all permutations of a string
Time complexity of printing all permutations of a string. void perm(String str){ perm(str, ""); } void perm(String str, String prefix){ if(str.length() == 0){ System.out.println(prefix); } else{ for(int i = 0; i < str.length(); i++){ String rem = str.substring(0, i) + str.substring(i + 1); perm(r
3 min read
Generate permutations with only adjacent swaps allowed
Given a string on length N. You can swap only the adjacent elements and each element can be swapped atmost once. Find the no of permutations of the string that can be generated after performing the swaps as mentioned. Examples: Input : 12345 Output : 12345 12354 12435 13245 13254 21345 21354 21435 S
5 min read
Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
Given two integers A and B, the task is to generate and print a string str such that: str must only contain the characters 'a' and 'b'.str has length A + B and the occurrence of the character 'a' is equal to A and the occurrence of character 'b' is equal to BThe sub-strings "aaa" or "bbb" must not o
6 min read
Find n-th lexicographically permutation of a string | Set 2
Given a string of length m containing lowercase alphabets only. We need to find the n-th permutation of string lexicographic ally. Examples: Input: str[] = "abc", n = 3 Output: Result = "bac" All possible permutation in sorted order: abc, acb, bac, bca, cab, cba Input: str[] = "aba", n = 2 Output: R
11 min read
Different Ways to Generate Permutations of an Array
Permutations are like the magic wand of combinatorics, allowing us to explore the countless ways elements can be rearranged within an array. Whether you're a coder, a math enthusiast, or someone on a quest to solve a complex problem, understanding how to generate all permutations of an array is a va
6 min read