Search code examples
c#algorithmtraversal

How to traverse through char array


Function receives char[,].

For example if it it takes

000
LAD
0B0

Traversing should print out all possible combinations of non-zero chars:

L
LA
LAD
LAB
A
AL
AB
AD

and so on

private void Traverse(char[,] area) 
{

}

Solution

  • The easiest way is to write a recursive function with two strings, initial and output. I assume you want combinations, not permutation so it's a little easier. The base case is checking if initial is empty, then print out the output. The step is removing a character from initial and calling the recursive function twice, one with the unchanged output and one with the removed character added into the output. If the removed character is 0, however, then you only call the function once (removing the 0 without adding anything to the output.)