Created
February 26, 2016 17:29
-
-
Save jakehawken/759d0fd7523f12b6a329 to your computer and use it in GitHub Desktop.
Convert a string like "aabcccccbba" to one like "2a1b5c2b1a" more efficiently than this one: https://gist.github.com/jakehawken/482315e0a7d07fc15b1a
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
- (void)letterCounter:(NSString *)originalString | |
{ | |
NSMutableString *outputString = [NSMutableString new]; | |
NSInteger stringIndex = 0; | |
while (stringIndex < originalString.length) | |
{ | |
unichar currentChar = [originalString characterAtIndex:stringIndex]; | |
NSInteger characterCount = 1; | |
for (NSInteger i = stringIndex + 1; i < originalString.length; i++) | |
{ | |
if ([originalString characterAtIndex:i] == currentChar) | |
{ | |
characterCount++; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
[outputString appendString:[NSString stringWithFormat:@"%ld%c", characterCount, currentChar]]; | |
stringIndex += characterCount; | |
} | |
printf("%s\n", [outputString UTF8String]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment