Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just for starters, create a <code>NSCharacterSet</code> from your <code>letters</code> and call this function before calling the lengthy function. This is a faster check to reduce the possibilities and it should improve your computation time.</p> <pre><code>NSCharacterSet* lettersSet = [NSCharacterSet characterSetWithCharactersInString:letters]; - (BOOL)isWordValid:(NSString*)word forLettersSet:(NSCharacterSet*)lettersSet { if ([word length] &lt; 2) return NO; NSCharacterSet* wordLetters = [NSCharacterSet characterSetWithCharactersInString:word]; return [lettersSet isSupersetOfSet:wordLetters]; } </code></pre> <p>Ideally, your word database should have precomputed the letter count for each word (e.g. <code>every = {e=2, r=1, v=1, y=1}</code> and your should work only with these structures. Note that the order of letters is not significant - using this fact can greatly improve the performance of your algorithm.</p> <p>You can also try to create a Core Data database - every word will be one record with a number field for every letter. Then you can create a request which will return you the available words VERY fast. (Of course, the database will probably take bigger amount of space).</p> <p>EDIT: Now I have found out <code>NSCountedSet</code> class, so let's try to use it:</p> <pre><code>-(NSCountedSet*)lettersSetFromString:(NSString*)string { NSCountedSet* letters = [NSCountedSet set]; [string enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [letters addObject:substring]; }]; } -(NSMutableArray *)validWordsForLetters:(NSString*)letters { NSCountedSet* lettersSet = [self lettersSetFromString:letters]; NSMutableArray* results = [NSMutableArray array]; for (NSString* word in wordList) { if ([word length] &lt; 2) { continue; } NSCountedSet* wordLettersSet = [self lettersSetFromString:word]; if ([wordLettersSet isSubsetOfSet:lettersSet]) { [results addObject:word]; } } return results; }</code></pre> <p>Generating the counted sets for every word beforehand will help the perfomance a lot. Using a Core Data database will still be faster and better for the OS memory.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload