I can't seem to really think of a way to solve this one, can't get my brain around it for some reason. The problem I'm trying to solve is this:
For a puzzle-solver type algorithm, I'm pulling the duplicate letters as a substring of an NSString. Let's say the user enters "RBEEEIOOUUU" I'm pulling just the duplicates from their string, and want to see every possible combination of those duplicates, not so much positionally in the string but just by varying the repetition count.. (for this problem position doesn't matter, I alphabetize it later..)
So, given the string EEEOOUUU, I want to derive a set of all possible combinations of the string, based on essentially varying the duplicates,
Given this example, all possible strings with 3 or Less E's, 2 or Less O's and 3 or Less U's.
So, just off the top of my head, I'd want to return
EEEOOUUU (the source string) EEEOUUU EEEOOUU EEEOOU
EEOOUUU EEOUUU EEOOUUU EEOUU EEOU EOOUUU ....... and so on down...
Recursion owns me on this one for some reason, I can't even visualize the solution. I have algorithms for permutations of sets of letters of fixed length but that doesn't help here or at least my poor sleep deprived brain can't apply them.
Any help or suggestions someone feeling generous may be willing to provide, I'll be indebted to you..
For the topic of conversation here's something bad I just hacked up that probably can be all thrown away in lieu of something.... of course this assumes some calls that'd I'd flesh in later for returning only the dupes, etc. Not thrilled with this as a use of arrays but it's not something that's going to be repeated for a bunch of entry terms.
//probably the most inefficent way of doign this, sorry for my level of fail.
-(NSMutableArray*) getDuplicatePermutations:(NSMutableArray*)workingArray workingWord:(NSMutableString*)workingWord currentLetter:(NSString*)currentLetter{
NSArray *dupeArray = [self getDuplicateLetters]; //this returns only the letters with an occurrence >1 so in EEEIIOOOT, it returns "E", I", "O"
if (workingWord==nil){workingWord = [[NSMutableString alloc] init];
for (NSString *thisLetter in dupeArray)
{
for (NSString* thisLetterStuff in [self possibleDupePermutationsForLetter:thisLetter theWord:self]){ ////this thing returns NSArray of NSStrings like so "EEE","EE", "E"
workingWord = [workingWord stringByAppendingString:thisLetterStuff];
if ([thisLetter isEqualToString:[dupeArray lastObject]]) { ///do... something.. at the lowest depth...
[workingArray addObject:workingWord];
workingWord = @"";
}
workingArray = [self getDuplicatePermutations:workingArray workingWord:workingWord currentLetter:thisLetter]; //I can haz recursion? No idea where to do it,actually.
}
}
}
return workingArray; ///ostensibly an array filled with crap the looping mess builds
}