Pergunta de entrevista da empresa Yelp

Given a string, print true or false if you can create a palindrome with it.

Resposta da entrevista

Sigiloso

27 de jun. de 2015

// Note that character enumeration in case of characters that need more than a byte should be done in another way + (BOOL)canGeneratePalindrome:(NSString *)string { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; NSInteger oddOccurences = 0; NSInteger stringLength = string.length; for (NSUInteger i = 0; i < stringLength; i++) { NSString *character = [string substringWithRange:NSMakeRange(i, 1)]; NSInteger occurences = [dictionary[character] integerValue]; oddOccurences = occurences % 2 == 0 ? oddOccurences + 1 : oddOccurences - 1; dictionary[character] = @(occurences+1); } return stringLength % 2 == 0 ? oddOccurences == 0 : oddOccurences == 1; }