Last Updated: 10 Jul, 2017

Print Permutations

Moderate
Asked in companies
SalesforceDunzoLivekeeping (An IndiaMART Company)

Problem statement

Given an input string (STR), print all possible permutations of the input string.

Note:
The input string may contain the same characters, so there will also be the same permutations.
The order of permutations doesn’t matter.
Input Format:
The only input line contains a string (STR) of alphabets in lower case
Output Format:
Print each permutations in a new line
Constraint:
1<=length of STR<=8
Time Limit: 1sec

Approaches

01 Approach

We can find all the permutations by Backtracking.

1:Fix a character then swap all the rest of the remaining character with a fixed character.

2:Then find all permutations for all remaining characters by the recursive call.

3:The base case for the recursion will be when there is only one character left unprocessed.

Below is the illustration to approach.