Last active
October 19, 2018 19:46
-
-
Save akahnn/88b713a2d5d4bd18127f8603a0f41d94 to your computer and use it in GitHub Desktop.
OL Coding Challenge
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
// | |
// ViewController.m | |
// OL_Coding_One | |
// | |
// Created by Abdullah Kahn on 10/19/18. | |
// Copyright © 2018 Sifted. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSString *letter = [NSString stringWithFormat:@"abc"]; | |
NSString *magazine = [NSString stringWithFormat:@"abcd"]; | |
BOOL doesMagazineContainLetter = [self checkIfLetterInMagazine:letter magazine:magazine]; | |
if (doesMagazineContainLetter) {NSLog(@"TRUE");} else {NSLog(@"FALSE");} | |
} | |
-(BOOL)checkIfLetterInMagazine:(NSString *)letter magazine:(NSString *)magazine { | |
NSMutableDictionary *dict = [NSMutableDictionary new]; | |
for (int i=0;i<letter.length;i++) { | |
NSString *letterAtIndex = [NSString stringWithFormat:@"%c",[letter characterAtIndex:i]]; | |
if ([dict objectForKey:letterAtIndex] == nil) { | |
[dict setObject:[NSNumber numberWithInt:0] forKey:letterAtIndex]; | |
} else { | |
NSString *countString = [dict objectForKey:letterAtIndex]; | |
int count = countString.intValue; | |
[dict setObject:[NSNumber numberWithInt:++count] forKey:letterAtIndex]; | |
} | |
} | |
for (int i=0;i<magazine.length;i++) { | |
NSString *letterAtIndex = [NSString stringWithFormat:@"%c",[magazine characterAtIndex:i]]; | |
if ([dict objectForKey:letterAtIndex]) { | |
NSString *countString = [dict objectForKey:letterAtIndex]; | |
int count = countString.intValue; | |
if (count == 0) { | |
[dict removeObjectForKey:letterAtIndex]; | |
} else { | |
[dict setObject:[NSNumber numberWithInt:--count] forKey:letterAtIndex]; | |
} | |
} | |
} | |
if (dict.allKeys.count == 0) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment