Created
March 27, 2015 16:30
-
-
Save gegagome/bd46ba25f8835e36e00e to your computer and use it in GitHub Desktop.
Hamming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Hamming.h" | |
@implementation Hamming | |
+ (NSUInteger) compute:(NSString *)firstString against:(NSString *)secondString { | |
int hammingDistance = 0; | |
if ([firstString length] <= [secondString length]) { | |
hammingDistance = (int)[self loopForStringBeginningWithShort:firstString withLong:secondString]; | |
} else { | |
hammingDistance = (int)[self loopForStringBeginningWithShort:secondString withLong:firstString]; | |
} | |
return hammingDistance; | |
} | |
+ (NSUInteger) loopForStringBeginningWithShort:(NSString *)shortStr withLong:(NSString *)longStr { | |
int distance = 0; | |
for(int i = 0; i < [shortStr length]; i++) { | |
NSString* a = [NSString stringWithFormat:@"%c", [shortStr characterAtIndex:i]]; | |
NSString* b = [NSString stringWithFormat:@"%c", [longStr characterAtIndex:i]]; | |
if (![a isEqual:b]) { | |
distance++; | |
} | |
} | |
return (NSUInteger)distance; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment