Skip to content

Instantly share code, notes, and snippets.

@kchronis
Last active January 8, 2017 19:09
Show Gist options
  • Select an option

  • Save kchronis/7109453 to your computer and use it in GitHub Desktop.

Select an option

Save kchronis/7109453 to your computer and use it in GitHub Desktop.
-(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];
}
}
@zeeshankhan
Copy link

This code doesn't work.

@petromobile
Copy link

This cod is not help for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment