I need a function that does the same thing as the following:
static public IEnumerable<string> permute(string word)
{
if (word.Length > 1)
{
char character = word[0];
foreach (string subPermute in permute(word.Substring(1)))
{
for (int index = 0; index <= subPermute.Length; index++)
{
string pre = subPermute.Substring(0, index);
string post = subPermute.Substring(index);
if (post.Contains(character))
continue;
yield return pre + character + post;
}
}
}
else
{
yield return word;
}
}
I tried this
-(NSString *) permute:(NSString *)str
{
NSLog(@"permuting %@", str);
NSString *permutedString = [[NSString alloc] init];
if (str.length > 1) {
NSString *subPermute = [[NSString alloc] init];
for (subPermute in [str substringFromIndex:1])
{
for (int i = 0; i <= [subPermute length];i++) {
NSString *pre = [[NSString alloc] initWithString:[subPermute substringToIndex:i]];
NSString *post = [[NSString alloc] initWithString:[subPermute substringFromIndex:i]];
return [pre stringByAppendingFormat:@"%@%@", [str substringToIndex:1], post];
}
}
}
else {
NSLog(@"permuted string = %@", permutedString);
return permutedString;
}
return permutedString;
}
However I get a warning message for the following line:
for (subPermute in [str substringFromIndex:1])
How should this be changed?