Created
January 22, 2016 21:34
-
-
Save ygit/0cc264d6167f0dac8629 to your computer and use it in GitHub Desktop.
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)solution{ | |
NSArray *list = @[@"end", @"back", @"and", @"the", @"po", @"pu", @"lar", @"face"]; //populate with any list of strings here | |
NSString *inputStr = @"facebackandthethethefacebackpopularlarpopu"; //or input any string here | |
BOOL canBeFormed = [self checkForString:inputStr.mutableCopy inList:list]; | |
if (canBeFormed) { | |
NSLog(@"YES! %@ can be formed from the list.", inputStr); | |
} | |
else{ | |
NSLog(@"NO! %@ cannot be formed from the list", inputStr); | |
} | |
} | |
- (BOOL)checkForString:(NSMutableString *)inputStr inList:(NSArray *)list{ | |
for (NSString *str in list) { | |
NSRange range = [inputStr rangeOfString:str options:NSCaseInsensitiveSearch]; | |
if (range.location != NSNotFound) { | |
[inputStr deleteCharactersInRange:range]; | |
[self checkForString:inputStr inList:list]; | |
} | |
if ([inputStr isEqualToString:@""]) { | |
return YES; | |
} | |
} | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment