讓我提出一個不同的算法依賴於一個查詢,而不是通過一個數組的搜索。
設置:
迭代字典中的單詞。對於每個單詞,創建一個具有相同字符的字符串,按字母順序排序。使用此字符串作爲關鍵字,創建原始單詞數組的字典。
用法:
現在你可以做任何字符組合的檢查非常快:就像上面的字符排序和查找產生的向上鍵在地圖。
例子:
原始數組:(bond, Mary, army)
字謎查找圖:
{
bdno : (bond),
amry : (Mary, army),
}
使用這種地圖是非常快的檢查任何字的字謎。不需要迭代字典數組。
編輯:
我提出的算法分割在三個部分:
- 甲設置方法來構建從對象的字典中查找圖:
anagramMap
- 的方法,來計算字符按字符排序的鍵:
anagramKey
- 一種算法,可查找包含在九個字母單詞中的所有字符的排列並查找w地圖上的ords:
findAnagrams
。
這裏的所有三種方法作爲一個類別的上NSString
實現:
@interface NSString (NSStringAnagramAdditions)
- (NSSet *)findAnagrams;
@end
@implementation NSString (NSStringAnagramAdditions)
+ (NSDictionary *)anagramMap
{
static NSDictionary *anagramMap;
if (anagramMap != nil)
return anagramMap;
// this file is present on Mac OS and other unix variants
NSString *allWords = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableDictionary *map = [NSMutableDictionary dictionary];
@autoreleasepool {
[allWords enumerateLinesUsingBlock:^(NSString *word, BOOL *stop) {
NSString *key = [word anagramKey];
if (key == nil)
return;
NSMutableArray *keyWords = [map objectForKey:key];
if (keyWords == nil) {
keyWords = [NSMutableArray array];
[map setObject:keyWords forKey:key];
}
[keyWords addObject:word];
}];
}
anagramMap = map;
return anagramMap;
}
- (NSString *)anagramKey
{
NSString *lowercaseWord = [self lowercaseString];
// make sure to take the length *after* lowercase. it might change!
NSUInteger length = [lowercaseWord length];
// in this case we're only interested in anagrams 4 - 9 characters long
if (length < 4 || length > 9)
return nil;
unichar sortedWord[length];
[lowercaseWord getCharacters:sortedWord range:(NSRange){0, length}];
qsort_b(sortedWord, length, sizeof(unichar), ^int(const void *aPtr, const void *bPtr) {
int a = *(const unichar *)aPtr;
int b = *(const unichar *)bPtr;
return b - a;
});
return [NSString stringWithCharacters:sortedWord length:length];
}
- (NSSet *)findAnagrams
{
unichar nineCharacters[9];
NSString *anagramKey = [self anagramKey];
// make sure this word is not too long/short.
if (anagramKey == nil)
return nil;
[anagramKey getCharacters:nineCharacters range:(NSRange){0, 9}];
NSUInteger middleCharPos = [anagramKey rangeOfString:[self substringWithRange:(NSRange){4, 1}]].location;
NSMutableSet *anagrams = [NSMutableSet set];
// 0x1ff means first 9 bits set: one for each character
for (NSUInteger i = 0; i <= 0x1ff; i += 1) {
// skip permutations that do not contain the middle letter
if ((i & (1 << middleCharPos)) == 0)
continue;
NSUInteger length = 0;
unichar permutation[9];
for (int bit = 0; bit <= 9; bit += 1) {
if (i & (1 << bit)) {
permutation[length] = nineCharacters[bit];
length += 1;
}
}
if (length < 4)
continue;
NSString *permutationString = [NSString stringWithCharacters:permutation length:length];
NSArray *matchingAnagrams = [[self class] anagramMap][permutationString];
for (NSString *word in matchingAnagrams)
[anagrams addObject:word];
}
return anagrams;
}
@end
假設在一個名爲變量測試字符串nineletters
你會使用日誌的可能值:
for (NSString *anagram in [nineletters findAnagrams])
NSLog(@"%@", anagram);
我我面臨同樣的問題......但我沒有從答案算法中獲得所有可能的字典。 – 2013-10-10 08:06:30