Generate permutations with only adjacent swaps allowed Last Updated : 21 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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: Don't swap it, i.e. don't do anything with this character and move to the next character. Swap it. As it can be swapped with its adjacent, Swap it with the next character. Because each character can be swapped atmost once, we'll move to the position (i+2). 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. Implementation: 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; } 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 Python3 # Python3 program to generate permutations # with only one swap allowed. def findPermutations(string, index, n): if index >= n or (index + 1) >= n: print(''.join(string)) return # don't swap the current position findPermutations(string, 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. string[index], \ string[index + 1] = string[index + 1], \ string[index] findPermutations(string, index + 2, n) string[index], \ string[index + 1] = string[index + 1], \ string[index] # Driver Code if __name__ == "__main__": string = list("12345") n = len(string) findPermutations(string, 0, n) # This code is contributed by # sanjeev2552 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 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 ?> JavaScript <script> // JavaScript program to generate // permutations with only // one swap allowed. function findPermutations(str, index, n) { if (index >= n || index + 1 >= n) { document.write(str.join("") + "<br>"); 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); } function swap(arr, index) { var temp = arr[index]; arr[index] = arr[index + 1]; arr[index + 1] = temp; } // Driver code var str = "12345".split(""); var n = str.length; findPermutations(str, 0, n); </script> Output12345 12354 12435 13245 13254 21345 21354 21435 Comment More infoAdvertise with us Next Article Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed E Ekta Goel Improve Article Tags : Misc Strings Combinatorial DSA Goldman Sachs +1 More Practice Tags : Goldman SachsCombinatorialMiscStrings Similar Reads Number of swaps to sort when only adjacent swapping allowed Given an array arr[] of non negative integers. We can perform a swap operation on any two adjacent elements in the array. Find the minimum number of swaps needed to sort the array in ascending order. Examples : Input : arr[] = {3, 2, 1}Output : 3We need to do following swaps (3, 2), (3, 1) and (1, 2 13 min read Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed Given a string str containing only characters 0, 1, and 2, you can swap any two adjacent (consecutive) characters 0 and 1 or any two adjacent (consecutive) characters 1 and 2. The task is to obtain the minimum possible (lexicographically) string by using these swaps an arbitrary number of times. Exa 6 min read Generate all permutation of a set in Python Generating all permutations of a set in Python involves creating every possible ordered arrangement of its elements. Since sets are unordered collections, the first step is usually converting the set to a list to maintain a consistent order. For example, given the set {1, 2, 3}, the permutations inc 3 min read Generate all permutation of a set in Python Generating all permutations of a set in Python involves creating every possible ordered arrangement of its elements. Since sets are unordered collections, the first step is usually converting the set to a list to maintain a consistent order. For example, given the set {1, 2, 3}, the permutations inc 3 min read Generate a Permutation of 1 to N with no adjacent elements difference as 1 Given an integer N, the task is to construct a permutation from 1 to N where no adjacent elements have difference as 1. If there is no such permutation, print -1. Permutation from 1 to N has all the numbers from 1 to N present exactly once. Examples: Input: N = 5 Output: 4 2 5 3 1Explanation: As for 5 min read Generate all permutations of a string that follow given constraints 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. I 11 min read Like