A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
System.out.print("\nAll possible strings are : ");
// This code is contributed by adityapande88
print(answer, end =" ")
right_substr =s[i +1:]
rest =left_substr +right_substr
permute(rest, answer +ch)
# Driver Code
s =input("Enter the string : ")
print("All possible strings are : ")
# This code is contributed by Harshit Srivastava
Enter the string : abc
All possible strings are : abc acb bac bca cab cba
Time Complexity: O(n*n!) The time complexity is the same as the above approach, i.e. there are n! permutations and it requires O(n) time to print a permutation.
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes