Skip to content

Instantly share code, notes, and snippets.

@atifazad
Created October 30, 2017 16:18
Show Gist options
  • Save atifazad/7cfe4e6b1150c56e5b542cdca8ec313e to your computer and use it in GitHub Desktop.
Save atifazad/7cfe4e6b1150c56e5b542cdca8ec313e to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface Solution : NSObject
- (NSString *)countAndCall: (int) n;
- (NSString *)count:(int) n;
@end
@implementation Solution
- (NSString *)countAndCall: (int) n {
return [self count:n];
}
- (NSString *)count:(int) n {
if(n<=1) return @"1";
NSString *s=[self count:n-1];
NSMutableString *res=[[NSMutableString alloc] initWithString:@""];
int cnt=1;
for(int i=0; i<s.length; i++) {
char c1=[s characterAtIndex:i];
if(i==s.length-1 || c1 != [s characterAtIndex:i+1]) {
[res appendString:[NSString stringWithFormat:@"%d",cnt]];
[res appendString:[NSString stringWithFormat:@"%c",c1]];
cnt=1;
}
else {
cnt++;
}
}
return res;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
int cont;
do {
int n;
printf("Number: ");
scanf("%d", &n);
printf("\n");
Solution *sol=[[Solution alloc] init];
printf("%s\n",[[sol countAndCall:n] UTF8String]);
scanf("%d",&cont);
} while (cont!=0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment