Here you do not need recursion, you can list your patterns and treat them as binary numbers. For example, if your line has three zeros, you get:
0 000 ....0..0....0... 1 001 ....0..0....o... 2 010 ....0..o....0... 3 011 ....0..o....o... 4 100 ....o..0....0... 5 101 ....o..0....o... 6 110 ....o..o....0... 7 111 ....o..o....o...
You can implement this with bitwise operators or by treating the characters you want to replace, like an odometer.
The following is an implementation in C. I am not familiar with C #, and from other answers I can see that C # already has the appropriate standard classes for implementing what you want. (Although I am surprised that so many peolpe offer recursion here.)
So, this is more explanation or illustration of my comment on the question than recommendations for implementing your problem.
int binrep(char str[]) { int zero[40]; // indices of zeros int nzero = 0; // number of zeros in string int ncombo = 1; // number of result strings int i, j; for (i = 0; str[i]; i++) { if (str[i] == '0') { zero[nzero++] = i; ncombo <<= 1; } } for (i = 0; i < ncombo; i++) { for (j = 0; j < nzero; j++) { str[zero[j]] = ((i >> j) & 1) ? 'o' : '0'; } printf("%s\n", str); // should yield here } return ncombo; }
source share