My idea is similar with Rahul Tripathi's, however I did a tricky thing to make it work properly for duplicate elements. Anytime before we do a swap operation, we look back to check whether this element has show up before. If it is a duplicate element, we should not do the swap operation.The code is below with c++:
#include<iostream>
using namespace std;
bool isDupilicate(char input[],int start,int j)
{
bool ret=false;
for(int i=start;i<j;++i)
{
if(input[i]==input[j])
{
ret=true;
break;
}
}
return ret;
}
void swap(char& a,char &b)
{
char temp=a;
a=b;
b=temp;
}
void permutation(char input[],int start,int length)
{
if(start==length)
{
for(int i=0;i<length;++i)cout<<input[i];
cout<<endl;
}
else
{
for(int i=start;i<length;++i)
{
if(isDupilicate(input,start,i))continue;
swap(input[i],input[start]);
permutation(input,start+1,length);
swap(input[i],input[start]);
}
}
}
int main()
{
cout<<"Input for abb"<<endl;
char input[3]={'a','b','b'};
permutation(input,0,3);
cout<<"________________"<<endl;
cout<<"Input for abc"<<endl;
input[2]='c';
permutation(input,0,3);
getchar();
getchar();
}
Check this link to figure out how many unique patterns can be created! Hope it helps!