Skip to content

Instantly share code, notes, and snippets.

@gegagome
Created March 27, 2015 16:30
Show Gist options
  • Save gegagome/bd46ba25f8835e36e00e to your computer and use it in GitHub Desktop.
Save gegagome/bd46ba25f8835e36e00e to your computer and use it in GitHub Desktop.
Hamming
#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