Sunday, 20 November 2016

https://www.hackerrank.com/challenges/ctci-making-anagrams


http://www.devinline.com/2016/08/minimum-number-of-character-deletions.html





#import <Foundation/Foundation.h>
@interface FindAnagram:NSObject{
   
}
-(int) characterToDelete:(NSString*)str1 second:(NSString*)str2;
@end
@implementation FindAnagram{
}
    -(int) characterToDelete:(NSString*)str1 second:(NSString*)str2{
        int count = 0;
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        int length = str2.length;
        for(int i = 0 ; i < length ; i++){
            NSString *key = [str2 substringWithRange:NSMakeRange(i,1)];
            if([dict valueForKey:key] == nil){
                [dict setObject:[NSNumber numberWithInt:1]  forKey:key];
            }
            else{
                int value = [[dict valueForKey:key] intValue];
                //[dict setObjectForKey:[NSNumber numberWithInt:value+1]];
                 [dict setObject:[NSNumber numberWithInt:value+1]  forKey:key];
            }
        }
       
         for(int i = 0 ; i < str1.length ; i++){
            NSString *key = [str1 substringWithRange:NSMakeRange(i,1)];
           
            if([dict valueForKey:key] != nil){
                int value = [[dict valueForKey:key] intValue];
                if(value!=0){
                    // [dict setObjectForKey:[NSNumber numberWithInt:value-1]];
                     [dict setObject:[NSNumber numberWithInt:value-1]  forKey:key];
                   
                }
                else{
                    count++;
                }
             
            }
            else{
              count++;
            }
        }
        int temp = 0;
        NSArray *arr = [dict allKeys];
       // NSLog(@"%@",dict);
        for(int i = 0 ;i < arr.count; i++){
             int value = [[dict valueForKey:[arr objectAtIndex:i]] intValue];
            temp = temp+ value;
        }
       
       
        return temp+count;
    }

@end
int main(int argc, const char * argv[]){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString* a;
    char* a_temp = (char *)malloc(512000 * sizeof(char));
    scanf("%s",a_temp);
    a = [NSString stringWithFormat:@"%s", a_temp];
    NSString* b;
    char* b_temp = (char *)malloc(512000 * sizeof(char));
    scanf("%s",b_temp);
    b = [NSString stringWithFormat:@"%s", b_temp];
    FindAnagram *obj = [[FindAnagram alloc] init];
    int result = [obj characterToDelete:a second:b];
    printf("%i",result);
    [pool drain];
    return 0;
}

No comments:

Post a Comment