You are not logged in.

#1 2006-04-24 01:47:47

Eliatamby
Member
Registered: 2005-05-06
Posts: 80

permutations of non-sequential string in C

Hi,

I'm just learning C now and am trying to get my head around this permutation pickle I'm in.  I can pretty simply write code that will give all permutations of N number of letters from A-z.  E.g., if N = 4, it will print out AAAA AAAB .... zzzz. This is pretty simple iteratively or recursively with a counter.

now, however, I want to enter in a string that does not neccessary contain letters in sequence, and find all the possible relevant permutations of that string.  E.g. I input a string of BEHP, and  I want to find all the permutations of this combination.  I don't want BBBB, BBBE, etc., I want BEHP, BHPE, BPHE, etc. 

I think my current approach is severely limited by the counter method, as it's giving permutations that are just not relevant.

Also, can someone please confirm the maths on this.  If I have a string of 3 letters, ABC, the number of possible combinations is 3! right? 3 factorial?

Thanks

Offline

#2 2006-04-24 03:40:00

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: permutations of non-sequential string in C

I don't know C, but have a couple of logic-based suggestions.  I believe you are right in your permutations calculation of 3!.  To solve your problem of combinations, could you do something like put each character into an array (which is ordered by index) and then use a similar technique to the one you used for the other set of letters but base it on the index number?

Offline

#3 2006-04-24 06:01:48

Eliatamby
Member
Registered: 2005-05-06
Posts: 80

Re: permutations of non-sequential string in C

Thanks elastic, I think that will work PERFECTLY!

Man, I've got to get used to thinking like a programmer.

Offline

Board footer

Powered by FluxBB