Permutation of a string II

Write method to print all permutation of a string without duplication. 

C++:
01 void permutation(char* a, const int k, const int m)
02 {
03     if(k==m){
04         for(int i=0;i<=m;i++)
05             cout<<a[i]<<” “;
06         cout<<endl;
07     }
08     else{
09         map <char,int> e;
10         for(int i=k;i<=m;i++){
11             if(e.find(a[i])!=e.end())
12                 continue;
13             e[a[i]]=1;
14             swap(a[i],a[k]);
15             permutation(a,k+1,m);
16             swap(a[i],a[k]);
17         }
18     }
19 }
Advertisements
This entry was posted in Backtrack, Recursive, String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s