Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Permutations of a String
MEDIUM
15 mins
39 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Permutations of a String

Contributed by
Ankit Kharb
Medium
yellow-spark
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share
39 upvotes

Problem Statement

You are given a string 'STR' consisting of lowercase English letters. Your task is to return all permutations of the given string in lexicographically increasing order.

String A is lexicographically less than string B, if either A is a prefix of B (and A ≠ B), or there exists such i (1 <= i <= min(|A|, |B|)), that A[i] < B[i], and for any j (1 <= j < i) A[i] = B[i]. Here |A| denotes the length of the string A.

For example :

If the string is “bca”, then its permutations in lexicographically increasing order are { “abc”, “acb”, “bac”, “bca”, “cab”, “cba” }.
Note:
Given string contains unique characters.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
3
abc
bc
c
Sample Output 1:
abc acb bac bca cab cba
bc cb
c
Explanation for Sample Input 1:
In the 1st test case, there are 6 permutations of the given string.
In the 2nd test case, there are 2 permutations of the given string.
In the 3rd test case, there is only 1 permutation of the given string.
Sample Input 2:
1
xyz
Sample Output 2:
xyz xzy yxz yzx zxy zyx 
Reset Code
Full screen
Auto
copy-code
Console