Tricky Permutations doubt1

The code is not giving correct output for the string “AACC” and I am able to figure out the correct method. Please help.

Hello @Rooopak_Sharma,

The condition you have applied to avoid redundancy is not appropriate:
if(j==i || ch[j]!=ch[i])

Solution:
bool should_swap(char ch[],int i,int j){
for(int k=i;k<j;k++){
if(ch[j]==ch[k]){
return false
}
}
return true
}

if(should_swap(ch,i,j))

In the above logic we are checking if we should swap or not the ch[j] with ch[i].

  1. If ch[j] has already occurred between the index [i,j) then we won’t swap, as it will create a duplicate copy. This is because if that element has already occurred before jth index and after ith index, then it means that element present at ch[j] has already swapped with the element at ch[i].

  2. Example:
    DABAC
    Suppose i=0 and j=3,
    then ch[j] has already occurred before ch[1]. So, if we would swap then it will make a copy of previously obtained string, when we swapped ch[0] with ch[1].

Hope, this would help.
Give a like, if you are satisfied.