Given an array of words, write a method that determines whether there are any
words in this array that are anagrams of each other.
Sample #1: @[@"bag", @"bat", @"tab"]; // output TRUE
Sample #2: @[@"gab", @"bat", @"laf"]; // output FALSE
#include <Foundation/Foundation.h>
// .h
@interface Solution : NSObject
+ (BOOL) checkAnagram: (NSArray*)words;
@end
// .m
@interface Solution ()
@end
@implementation Solution
+ (BOOL) checkAnagram: (NSArray*)words {
if (words == nil) {
return NO;
}
NSMutableSet* dict = [NSMutableSet set];
for (NSString* word in words) {
NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString: word];
if ([dict containsObject: characterSet]) {
return YES;
}
[dict addObject: characterSet];
}
return NO;
}
@end
int main (void) {
@autoreleasepool {
NSArray* words = @[@"eat", @"tea", @"ate"];
NSLog(@"%@", [Solution checkAnagram: words]);
}
return 0;
}