Back to Practice Dashboard
Python BasicsEasy

Print All Permutations of a String

Learn how to solve the 'Print All Permutations of a String' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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.

Constraints
  • 1 <= len(s) <= 8
  • s contains only lowercase English letters

Examples

Example 1
Input
s = 'abc'
Output
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
Explanation

All 3! = 6 rearrangements of 'a', 'b', 'c', sorted alphabetically.

Example 2
Input
s = 'ab'
Output
['ab', 'ba']
Explanation

Two characters give 2! = 2 permutations.

Example 3
Input
s = 'aab'
Output
['aab', 'aba', 'baa']
Explanation

There are only 3 unique permutations since 'a' is repeated.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor