(code below regarding my question)
Per this question . I used the Pegolon approach to create all possible permutations of a group of characters inside an NSString. However, I'm now trying to get it to not just generate ANAGRAM, which is a permutation of the same length, but with all possible combinations (any length) of characters in a string.
Does anyone know how I would modify the following code to make it do this? This is very similar to: Generate All Permutations of All Lengths - but (fearing that they need an answer to their homework), they didnβt leave the code. I have a sample of what I thought would do it at the bottom of this article ... but it is not.
Thus, the code, as is, generates the , teh , hte , het , eth and eht when specifying the . I need the following lines: t , h , e , th , ht , te , he (etc.) In addition to the above 3 character combinations.
How to change this, please. (ps: There are two methods to this. I added allPermutationsArrayofStrings to return the results as strings, for example, I want them, and not just an array of characters in another array). I assume that magic will happen in pc_next_permutation anyway - but I thought I mentioned it.
In NSArray + Permutation.h
in NSArray + Permutation.m:
#define MAX_PERMUTATION_COUNT 20000 NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size); NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size) {
My code, which I thought would fix this:
for ( NSInteger i = 1; i <= theCount; i++) { NSRange theRange2; theRange2.location = 0; theRange2.length = i; NSLog(@"Location: %i (len: %i) is: '%@'",theRange2.location,theRange2.length,[array subarrayWithRange:theRange2]); NSArray *allWordsForThisLength = [[array subarrayWithRange:theRange2] allPermutationsArrayofStrings]; for (NSMutableString *theString in allWordsForThisLength) { NSLog(@"Adding %@ as a possible word",theString); [allWords addObject:theString]; }
I know that this would not be the most effective ... but I tried to check.
Here is what I got:
2011-07-07 14:02:19.684 TA[63623:207] Total letters in word: 3 2011-07-07 14:02:19.685 TA[63623:207] Location: 0 (len: 1) is: '( t )' 2011-07-07 14:02:19.685 TA[63623:207] Adding t as a possible word 2011-07-07 14:02:19.686 TA[63623:207] Location: 0 (len: 2) is: '( t, h )' 2011-07-07 14:02:19.686 TA[63623:207] Adding th as a possible word 2011-07-07 14:02:19.687 TA[63623:207] Adding ht as a possible word 2011-07-07 14:02:19.688 TA[63623:207] Location: 0 (len: 3) is: '( t, h, e )' 2011-07-07 14:02:19.688 TA[63623:207] Adding the as a possible word 2011-07-07 14:02:19.689 TA[63623:207] Adding teh as a possible word 2011-07-07 14:02:19.690 TA[63623:207] Adding hte as a possible word 2011-07-07 14:02:19.691 TA[63623:207] Adding het as a possible word 2011-07-07 14:02:19.691 TA[63623:207] Adding eth as a possible word 2011-07-07 14:02:19.692 TA[63623:207] Adding eht as a possible word
As you can see, not a single, not two literal words - I pull out my hair! (and I have nothing to save!)