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! (factorial of n).
itertools module in Python provides a simple function called permutations that can generate all possible permutations of a string. This method is the easiest and most efficient, especially when working with built-in Python libraries.
importitertoolss="GFG"li=[''.join(p)forpinitertools.permutations(s)]print(li)
Explanation:
Recursion repeatedly breaks the problem into smaller parts. In string permutation, it means choosing one character and recursively arranging the remaining characters, generating all possible orderings systematically.
defpermute(s,s2):iflen(s)==0:print(s2,end=' ')returnforiinrange(len(s)):char=s[i]left_s=s[0:i]right_s=s[i+1:]rest=left_s+right_spermute(rest,s2+char)s1="GFG"s2=""permute(s1,s2)
GFG GGF FGG FGG GGF GFG
Generate all permutation of a set in PythonPrint all Unique permutations of a given string.
S
Python Introduction
Input and Output in Python
Python Variables
Python Operators
Python Keywords
Python Data Types
Conditional Statements in Python
Loops in Python - For, While and Nested Loops
Python Functions
Recursion in Python
Python Lambda Functions
Python String
Python Lists
Python Tuples
Python Dictionary
Python Sets
Python Arrays
List Comprehension in Python
Python OOP Concepts
Python Exception Handling
File Handling in Python
Python Database Tutorial
Python MongoDB Tutorial
Python MySQL
Python Packages
Python Modules
Python DSA Libraries
List of Python GUI Library and Packages
NumPy Tutorial - Python Library
Pandas Tutorial
Matplotlib Tutorial
Python Seaborn Tutorial
StatsModel Library - Tutorial
Learning Model Building in Scikit-learn
TensorFlow Tutorial
PyTorch Tutorial
Flask Tutorial
Django Tutorial | Learn Django Framework
Django ORM - Inserting, Updating & Deleting Data
Templating With Jinja2 in Flask
Django Templates
Python | Build a REST API using Flask
How to Create a basic API using Django Rest Framework ?
Python Quiz
Python Coding Practice
Python Interview Questions and Answers