Skip to content

Instantly share code, notes, and snippets.

@ygit
Created January 22, 2016 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygit/0cc264d6167f0dac8629 to your computer and use it in GitHub Desktop.
Save ygit/0cc264d6167f0dac8629 to your computer and use it in GitHub Desktop.
- (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