- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
When it is required to print all the permutations of a string in lexicographic order using recursion, a method is defined, that uses the ‘for’ loop to iterate over the sequence of elements, and use the ‘join’ method to join the elements.
Below is the demonstration of the same −
Example
from math import factorial def lexicographic_permutation_order(s): my_sequence = list(s) for _ in range(factorial(len(my_sequence))): print(''.join(my_sequence)) next = next_in_permutation(my_sequence) if next is None: my_sequence.reverse() else: my_sequence = next def next_in_permutation(my_sequence): if len(my_sequence) == 0: return None next = next_in_permutation(my_sequence[1:]) if next is None: my_sequence[1:] = reversed(my_sequence[1:]) q = 1 while q < len(my_sequence) and my_sequence[0] > my_sequence[q]: q += 1 if q == len(my_sequence): return None my_sequence[0], my_sequence[q] = my_sequence[q], my_sequence[0] return my_sequence else: return [my_sequence[0]] + next my_input = input('Enter a string : ') print("The string is :") print(my_input) print("The method is being called...") lexicographic_permutation_order(my_input)
Output
Enter a string : hey The string is : hey The method is being called... hey hye yeh yhe hey hye
Explanation
The required packages are imported.
A method named ‘lexicographic_permutation_order’ is defined that helps find the lexicographic order of elements.
The method ‘next_in_permutation’ helps determine the next permutation in the string.
A string is entered by the user, and is displayed on the console.
The method is called by passing this string as a parameter.
The output is displayed on the console.
- Related Articles
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Print all permutations in sorted (lexicographic) order in C++
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Print all permutations of a given string
- Print all permutations of a string in Java
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all palindrome permutations of a string in C++
- Java Program to print distinct permutations of a string
- Python program to get all permutations of size r of a string
- Print first n distinct permutations of string using itertools in Python
- All permutations of a string using iteration?
- Python Program to Reverse a String Using Recursion
- Print all distinct permutations of a given string with duplicates in C++
- Python Program to Reverse a String without using Recursion

Advertisements