# 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

Source: Goldman Sachs Interview

Consider any i-th character in the string. There are two possibilities for this character:

1.) Don’t swap it, i.e. don’t do anything with this character and move to the next character.

2.) Swap it. As it can be swapped with its adjacent,

……..a.) Swap it with the next character. Because each character can be swapped atmost once, we’ll move to the position (i+2).

……. b.) Swap it with the previous character – we don’t need to consider this case separately as i-th character is the next character of (i-1)th which is same as the case 2.a.

## C++

`// CPP program to generate permutations with only ` `// one swap allowed. ` `#include <cstring> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `void` `findPermutations(` `char` `str[], ` `int` `index, ` `int` `n) ` `{ ` ` ` `if` `(index >= n || (index + 1) >= n) { ` ` ` `cout << str << endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// don't swap the current position ` ` ` `findPermutations(str, index + 1, n); ` ` ` ` ` `// Swap with the next character and ` ` ` `// revert the changes. As explained ` ` ` `// above, swapping with previous is ` ` ` `// is not needed as it anyways happens ` ` ` `// for next character. ` ` ` `swap(str[index], str[index + 1]); ` ` ` `findPermutations(str, index + 2, n); ` ` ` `swap(str[index], str[index + 1]); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `char` `str[] = { ` `"12345"` `}; ` ` ` `int` `n = ` `strlen` `(str); ` ` ` `findPermutations(str, 0, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to generate permutations with only ` `// one swap allowed. ` ` ` `class` `GFG { ` ` ` ` ` `static` `void` `findPermutations(` `char` `str[], ` `int` `index, ` `int` `n) { ` ` ` `if` `(index >= n || (index + ` `1` `) >= n) { ` ` ` `System.out.println(str); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// don't swap the current position ` ` ` `findPermutations(str, index + ` `1` `, n); ` ` ` ` ` `// Swap with the next character and ` ` ` `// revert the changes. As explained ` ` ` `// above, swapping with previous is ` ` ` `// is not needed as it anyways happens ` ` ` `// for next character. ` ` ` `swap(str, index); ` ` ` `findPermutations(str, index + ` `2` `, n); ` ` ` `swap(str, index); ` ` ` `} ` ` ` ` ` `static` `void` `swap(` `char` `arr[], ` `int` `index) { ` ` ` `char` `temp = arr[index]; ` ` ` `arr[index] = arr[index + ` `1` `]; ` ` ` `arr[index + ` `1` `] = temp; ` ` ` `} ` `// Driver code ` ` ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `char` `str[] = ` `"12345"` `.toCharArray(); ` ` ` `int` `n = str.length; ` ` ` `findPermutations(str, ` `0` `, n); ` ` ` `} ` `} ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to generate permutations with only ` `// one swap allowed. ` `using` `System; ` `public` `class` `GFG { ` ` ` ` ` `static` `void` `findPermutations(` `char` `[]str, ` `int` `index, ` `int` `n) { ` ` ` `if` `(index >= n || (index + 1) >= n) { ` ` ` `Console.WriteLine(str); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// don't swap the current position ` ` ` `findPermutations(str, index + 1, n); ` ` ` ` ` `// Swap with the next character and ` ` ` `// revert the changes. As explained ` ` ` `// above, swapping with previous is ` ` ` `// is not needed as it anyways happens ` ` ` `// for next character. ` ` ` `swap(str, index); ` ` ` `findPermutations(str, index + 2, n); ` ` ` `swap(str, index); ` ` ` `} ` ` ` ` ` `static` `void` `swap(` `char` `[]arr, ` `int` `index) { ` ` ` `char` `temp = arr[index]; ` ` ` `arr[index] = arr[index + 1]; ` ` ` `arr[index + 1] = temp; ` ` ` `} ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main() { ` ` ` `char` `[]str = ` `"12345"` `.ToCharArray(); ` ` ` `int` `n = str.Length; ` ` ` `findPermutations(str, 0, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to generate permutations ` `// with only one swap allowed. ` ` ` `function` `findPermutations(` `$str` `, ` `$index` `, ` `$n` `) ` `{ ` ` ` `if` `(` `$index` `>= ` `$n` `|| (` `$index` `+ 1) >= ` `$n` `) ` ` ` `{ ` ` ` `echo` `$str` `, ` `"\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// don't swap the current position ` ` ` `findPermutations(` `$str` `, ` `$index` `+ 1, ` `$n` `); ` ` ` ` ` `// Swap with the next character and ` ` ` `// revert the changes. As explained ` ` ` `// above, swapping with previous is ` ` ` `// is not needed as it anyways happens ` ` ` `// for next character. ` ` ` `list(` `$str` `[` `$index` `], ` ` ` `$str` `[` `$index` `+ 1]) = ` `array` `(` `$str` `[` `$index` `+ 1], ` ` ` `$str` `[` `$index` `]); ` ` ` ` ` `findPermutations(` `$str` `, ` `$index` `+ 2, ` `$n` `); ` ` ` `list(` `$str` `[` `$index` `], ` ` ` `$str` `[` `$index` `+ 1]) = ` `array` `(` `$str` `[` `$index` `+ 1], ` ` ` `$str` `[` `$index` `]); ` `} ` ` ` `// Driver code ` `$str` `= ` `"12345"` `; ` `$n` `= ` `strlen` `(` `$str` `); ` `findPermutations(` `$str` `, 0, ` `$n` `); ` ` ` `// This code is contributed by Sach_Code ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

12345 12354 12435 13245 13254 21345 21354 21435

This article is contributed by **ekta1994**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Longest common subsequence with permutations allowed
- Generate all cyclic permutations of a number
- Minimum insertions to form a palindrome with permutations allowed
- Minimum number of adjacent swaps to convert a string into its given anagram
- Generate all permutations of a string that follow given constraints
- Print distinct sorted permutations with duplicates allowed in input
- Iterative program to generate distinct Permutations of a String
- Permutations of string such that no two vowels are adjacent
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Largest permutation after at most k swaps
- Minimum Swaps for Bracket Balancing
- Form the largest palindromic number using atmost two swaps
- Minimum swaps required to convert one binary string to another