I have a simple recursive program to find all the distinct permutation of a given string. I am using a recursive function go()
to calculate the permutations and using a List<char[]>
to store the permuations. After a calculation, the function checks whether the permutation already exists in the List<char[]>
or not. If it doesn't, it gets added to the List<>.
Now here is the problem, the permutations are getting calculated perfectly but when i try to add them to the list, it't not adding the current value. Here is the code that does the adding.
List<char[]> l;
public void go(char[] list, int k, int m)
{
if (k == m)
{
Console.WriteLine(list); //print to check
l.Add(list); //add it to the list
}
// more code
}
Now for input=ABC, i get the following output..
ABC
ACB
BAC
BCA
CBA
CAB
which is correct because i am printing it directly. but when i print the list, i get.
ABC
ABC
ABC
ABC
ABC
ABC
Now i know i am making just a simple mistake somewhere but i can't figure out what. I have tried this in a bunch of different ways but nothing seems to work and i can't find any answer on the internet. Here is the complete code.