Given a string and a number, this function will produce all possible combinations of n letters from that string.
i.e. If I pass "abcd" and 3, then I should get the following output:
abc
abd
acd
bcd
This is the code
- (void)viewDidLoad {
[super viewDidLoad];
[self expand_combinations:@"abcd" arg2:@"" arg3:3];
}
-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
if(remain_depth==0)
{
printf("%s\n",[s UTF8String]);
return;
}
for(int k=0; k < [remaining_string length]; ++k)
{
s = [s stringByAppendingString:[[remaining_string substringFromIndex:k] substringToIndex:1]];
[self expand_combinations:[remaining_string substringFromIndex:k+1] arg2:s arg3:remain_depth - 1];
}
return;
}
Instead this is what it prints out
abc
abcd
abcd
abcd