Print All Permutations of a String
Detailed guide and Python implementation for the 'Print All Permutations of a String' problem.
Problem Statement
Write a function permutations(s) that takes a string s and returns a sorted list of all unique permutations of the characters in s. Each permutation is a rearrangement of all characters. The returned list must be sorted in lexicographic (alphabetical) order.
- •1 <= len(s) <= 8
- •s contains only lowercase English letters
Examples
s = 'abc'
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
All 3! = 6 rearrangements of 'a', 'b', 'c', sorted alphabetically.
s = 'ab'
['ab', 'ba']
Two characters give 2! = 2 permutations.
s = 'aab'
['aab', 'aba', 'baa']
There are only 3 unique permutations since 'a' is repeated.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.