Last active
January 8, 2017 19:09
-
-
Save kchronis/7109453 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -(NSArray *)arrayWithStringPermutations:(NSString *)myString{ | |
| NSInteger stringLength = [myString length]; | |
| if (stringLength < 2) { | |
| return [NSArray arrayWithObject:myString]; | |
| } | |
| else{ | |
| NSString *head = [myString substringFromIndex:1]; | |
| NSString *newBase = [myString substringToIndex:1]; | |
| NSArray *permutations = [self arrayWithStringPermutations:newBase]; | |
| NSMutableArray *permutationsArray = [NSMutableArray array]; | |
| for (NSInteger i = 0; i<[permutations count]; i++) { | |
| [self mergeHeadString:head withString:newBase addTo:permutationsArray]; | |
| } | |
| return permutationsArray; | |
| } | |
| } | |
| -(void)mergeHeadString:(NSString *)theString withString:(NSString *)base addTo:(NSMutableArray *)permutationsArray{ | |
| NSUInteger count = [base length]; | |
| while (count != 0) { | |
| count--; | |
| NSMutableString *mutBase = [base mutableCopy]; | |
| [mutBase insertString:theString atIndex:count]; | |
| [permutationsArray addObject:mutBase]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code doesn't work.