Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am stuck with a part in my program and need help.

The part where i am stuck is that i need to get all possible possibilities with specific characters.

Example:-
int NoOfSpaces = 3;
string[] CharForPossibilities = new string[] {'a','b'};

OUTPUT
outputStr[0] = "aaa";
outputStr[1] = "bbb";
outputStr[2] = "baa";
outputStr[3] = "aba";
outputStr[4] = "aab";
outputStr[5] = "bba";
outputStr[6] = "bab";
outputStr[7] = "abb";

i have no idea how to achieve this. Any code snippet would be appreciated.



Thanks in advance
Posted
Updated 12-Nov-14 3:12am
v3
Comments
agent_kruger 12-Nov-14 9:08am    
sir, the problem here is i have 1 extra requirement that is "NoOfSpaces" which can be 3 and CharForPossibilities can still be "a" & "b" and output will be "8" possiblities
[no name] 12-Nov-14 9:14am    
I don't understand it right: Does it mean if I have two chars and NoOfSpaces= 3 then an extra space Needs to be added to {'a', 'b', ' '}? And if this right, what with NoOfSpaces= 4 will happen?

After extend your example my question above is history :)
agent_kruger 12-Nov-14 9:16am    
sir please see the updated example in the question.
[no name] 12-Nov-14 9:28am    
If you sort it in another way:

aaa
aab
abb
aba
bbb
baa
bba
bab

it Looks like permutation of {'a','b'} repeated one time with preceeding 'a' and one time with preceeding 'b'.

But was is the General logic behind it, if NoOfSpaces = 4 or what else...

1 solution

Devolves into a simplified knapsack problem. All your items are the same size/weight and you can have multiples of each to fill the sack.

http://en.wikipedia.org/wiki/Knapsack_problem#Unbounded_knapsack_problem[^]
 
Share this answer
 
Comments
agent_kruger 12-Nov-14 9:36am    
sir can you simplify the code of c to c# as i am have forgotten syntax of c and cannot find a c to c# convertor
PIEBALDconsult 12-Nov-14 9:42am    
No.
agent_kruger 12-Nov-14 9:46am    
please sir a little help can help me to proceed further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900