The Wayback Machine - https://web.archive.org/web/20240712112925/https://www.geeksforgeeks.org/python-program-to-print-all-permutations-of-a-given-string/
Open In App

Python Program to print all permutations of a given string

Last Updated : 13 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
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

Here is a solution that is used as a basis in backtracking.

NewPermutation

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|)



Similar Reads

Python Program for Anagram Substring Search (Or Search for all permutations)
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m. Expected time complexity is O(n) Examples: 1) Input: txt[] = "BACDGABCDA" pat[] = "ABCD" Output: Found at Index 0 Found at Index 5 Found a
4 min read
Python Program for Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After step 1, n must be
6 min read
Python | All possible permutations of N lists
Computing permutations is always a necessary task in many of the practical applications and a concept widely used in Mathematics to achieve solutions to many practical problems. Lets discuss certain ways in which one can perform the task of getting all the permutations of N lists. Method #1 : Using list comprehension List comprehension can be used
4 min read
Python | Remove all duplicates and permutations in nested list
Given a nested list, the task is to remove all duplicates and permutations in that nested list. Input: [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] Output: {(-11, 0, 11), (-11, -11, 2)} Input: [[-1, 5, 3], [3, 5, 0], [-1, 5, 3], [1, 3, 5], [-1, 3, 5], [5, -1, 3]] Output: {(1, 3, 5), (0, 3, 5), (-1, 3, 5)}
4 min read
Generate all possible permutations of words in a Sentence
Given a string S, the task is to print permutations of all words in a sentence. Examples: Input: S = “sky is blue”Output: sky is bluesky blue isis sky blueis blue skyblue sky isblue is sky Input: S =” Do what you love”Output:Do what you loveDo what love youDo you what loveDo you love whatDo love what youDo love you whatwhat Do you lovewhat Do love
13 min read
Python program to print all Strong numbers in given list
Given a list, write a Python program to print all the strong numbers in that list. Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Example for checking if number is Strong Number or not. Input: n = 145 Output: Yes Explanation: Sum of digit factorials = 1! + 4! + 5! = 1 + 24 + 120 = 145 Steps for chec
5 min read
Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers. Examples: Input: A = 378212, B = 78124590Output: 9 5 4 3 0Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers. Inpu
5 min read
Python Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are divisible by both 3 and 5. For this divide each n
2 min read
Python program to print the substrings that are prefix of the given string
Given a string, print all the possible substrings which are also the prefix of the given string. Examples: Input : ababc Output : a, ab, aba, abab, ababc, a, ab Input : abdabc Output : a, ab, abd, abda, abdab, abdabc, a, ab Approach: We use two variables: start and end to keep track of the current substring and follow the below conditions until sta
3 min read
Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are divisible by both 3 and 5. For this divide each n
9 min read
Article Tags :