Write a C program to print all permutations of a given string
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.
Source: Mathword(http://mathworld.wolfram.com/Permutation.html)
Below are the permutations of string ABC.
ABC, ACB, BAC, BCA, CAB, CBA
Here is a solution using backtracking.
# include <stdio.h> /* Function to swap values at two pointers */ void swap (char *x, char *y) { char temp; temp = *x; *x = *y; *y = temp; } /* Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string. */ void permute(char *a, int i, int n) { int j; if (i == n) printf("%s\n", a); else { for (j = i; j <= n; j++) { swap((a+i), (a+j)); permute(a, i+1, n); swap((a+i), (a+j)); //backtrack } } } /* Driver program to test above functions */ int main() { char a[] = "ABC"; permute(a, 0, 2); getchar(); return 0; }
Output:
ABC ACB BAC BCA CBA CAB
Algorithm Paradigm: Backtracking
Time Complexity: O(n*n!)
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
Related Topics:
- Given an array of strings, find if the strings can be chained to form a circle
- Given a sorted dictionary of an alien language, find order of characters
- Anagram Substring Search (Or Search for all permutations)
- Find Excel column name from a given column number
- Count Possible Decodings of a given Digit Sequence
- Check if a given string is a rotation of a palindrome
- Print all possible words from phone digits
- Printing Longest Common Subsequence
Writing code in comment? Please use ideone.com and share the link here.
- rohan
- ar
- SN
- Kai
- Pratham
- Javed
- Pratham
- SHANTANU AGRAWAL
- Ankit
- sonu
- Vignesh A
- Sudheer Reddy Jakkam
- Andria
- saurabh
- Charles
- Gaurav Ramesh
- mragrid
- Jingguo Yao
- mathsmaths
- DevilGeek
- http://atiqwhiz.blogspot.in/ atiq
- Sriharsha g.r.v
- Sriharsha g.r.v
- Gaurav Ramesh
- Guest
- derek
- Gaurav Ramesh
- akshita
- Gaurav Ramesh
- Sameer
- DarkProtocol
- GuestPost
- Puzzled
- Neha Garg
- Bharath G M
- Guy
- all izz well
- Bharath G M
- Nhan
- Silent
- Pranjal
- Marsha Donna
- Subrahmanyan Sankaran
- http://www.retrify.com irfanali
- vishal
- mahesh
- http://code.scottshipp.com Scott Shipp
- Sreenivas Doosa
- radhika
- Hammond
- Mridul
- Tunnu
- Deepesh
- Deepesh
- Shiv
- Shiv
- Shiv
- http://www.groupme.in Nikin
- Hammond
- Hammond
- Hammond
- Hammond
- Hammond
- googlybhai
- bharatJ
- Dharmendra Singh
- Dharmendra Singh
- Dharmendra Singh
- rahul732
- geek
- Vikash
- Vikash
- Chirag
- Chirag
- Chirag
- Chirag
- sreeram
- ChangingNameEveryTime
- Chirag
- aaryan
- Satyarth
- sumit saha
- gaya
- andrew
- arya
- user123
- Guddu sharma
- charanjeet
- Venki
- Vinayaka Bhat
- charanjeet
- hARRY
- kumar
- Sourodeep Roy
- Avinash
- niak
- guru
- http://blackbox-iphone.com Jailbreak and unlock your iPhone
- Venkatesh
- amit
- pratik
- PRATEEK
- faizan
- innosam
- navyasree
- coder
- pinku
- pinku
- sandy880
- Shaha
- mahendhar rao
- shantanu bhawre
- sura
- pps
- ravi
- ror
- Amitabh
- Ravinder Bishnoi & Yugarsi Ghosh
- Ravinder Bishnoi & Yugarsi Ghosh
- amit khoth
- Anand
- Sam
- rimu.nitrkl
- ricky
- crash
- Ahul
- Matrix
- world is round
- Shashank Mani Narayan
- rahul
- Phani Deepak
- cod3r
- cod3r
- http://www.divye.in Divye
- Anshul
- stackoverflow
- Anshul
- Ankul
- seeker7
- Anshu Bansal
- remo
- http://voxels.blogspot.com Sven Forstmann
- anom
- amit
- Ved
- http://srini156.blogspot.com Srinivas Iyengar
- http://srini156.blogspot.com Srinivas Iyengar