Python Program to print all permutations of a given string Last Updated : 13 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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(http://mathworld.wolfram.com/Permutation.html) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Here is a solution that is used as a basis in backtracking. Python3 # Python program to print all permutations # with duplicates allowed def toString(List): return ''.join(List) # Function to print permutations # of string # This function takes three parameters: # 1. String # 2. Starting index of the string # 3. Ending index of the string. def permute(a, l, r): if l == r: print (toString(a)) else: for i in range(l, r + 1): a[l], a[i] = a[i], a[l] permute(a, l + 1, r) # backtrack a[l], a[i] = a[i], a[l] # Driver code string = "ABC" n = len(string) a = list(string) permute(a, 0, n-1) # This code is contributed by Bhavya Jain Output: ABC ACB BAC BCA CBA CAB Algorithm Paradigm: Backtracking Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a permutation. Auxiliary Space: O(r - l) Note: The above solution prints duplicate permutations if there are repeating characters in the input string. Please see the below link for a solution that prints only distinct permutations even if there are duplicates in input.Print all distinct permutations of a given string with duplicates. Permutations of a given string using STL Another approach: Python3 # Python program to implement # the above approach def permute(s, answer): if (len(s) == 0): print(answer, end = " ") return for i in range(len(s)): ch = s[i] left_substr = s[0:i] right_substr = s[i + 1:] rest = left_substr + right_substr permute(rest, answer + ch) # Driver Code answer = "" s = input("Enter the string : ") print("All possible strings are : ") permute(s, answer) # This code is contributed by Harshit Srivastava Output: Enter the string : abc All possible strings are : abc acb bac bca cab cba Time Complexity: O(n*n!) The time complexity is the same as the above approach, i.e. there are n! permutations and it requires O(n) time to print a permutation. Auxiliary Space: O(|s|) Comment More infoAdvertise with us Next Article Python Program to print all permutations of a given string kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads 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 Python | Permutation of a given string using inbuilt function The task is to generate all the possible permutations of a given string. A permutation of a string is a rearrangement of its characters. For example, the permutations of "ABC" are "ABC", "ACB", "BAC", "BCA", "CAB", and "CBA". The number of permutations of a string with n unique characters is n! (fac 2 min read Print first n distinct permutations of string using itertools in Python Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook ok 2 min read Print all subsequences of a string in Python Given a string s of size n (1 ⤠n ⤠20), the task is to print all subsequences of string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.Examples: Input: s = "abc"Output: ["", "a", "b", "c", " 3 min read Output of Python Programs | Set 23 (String in loops) Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s 2 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read Python | All Permutations of a string in lexicographical order without using recursion Write a python program to print all the permutations of a string in lexicographical order. Examples: Input : python Output : hnopty hnopyt hnotpy hnotyp hnoypt ...... ytpnho ytpnoh ytpohn ytponh Input : xyz Output : xyz xzy yxz yzx zxy zyx Method 1: Using the default library itertools function permu 2 min read Python | Print all string combination from given numbers Given an integer N as input, the task is to print the all the string combination from it in lexicographical order. Examples: Input : 191 Output : aia sa Explanation: The Possible String digit are 1, 9 and 1 --> aia 19 and 1 --> sa Input : 1119 Output : aaai aas aki kai ks Approach: Get the Str 2 min read Python | SymPy Permutation.get_positional_distance() method Permutation.get_positional_distance() : get_positional_distance() is a sympy Python library function that calculates the positional distance between two permutations. Syntax : sympy.combinatorics.permutations.Permutation.get_positional_distance() Return : positional distance between two permutations 1 min read Python | SymPy Permutation.inversion_vector() Permutation.inversion_vector() : inversion_vector() is a sympy Python library function that returns the inversion_vector value of the permutation in argument. The inversion vector includes those elements whose value indicates the no. of elements in the permutation that are < it and lie on its rig 2 min read Like