Implement method oneEditApart that return boolean: true, if using one operations (insert or remove or replace) we can modify one string to get another. False otherwise. // Signature: boolean oneEditApart(String s1, String s2) // Allowing operations insert remove replace Example: oea("cat", "cut") => true // replace "u" -> "a" oea("cat", "cuts") => false // no operations oea("ca", "ca") => false // no operations oea("cats", "cat") => true // remove "s" oea("cat", "at") => true // insert "c" oea("cat", "cbat") => true // remove "b"
Anónimo
#import static BOOL oneRemoveApart(NSString *s1, NSString *s2) { if (s1.length == 0) { return NO; } NSMutableString *s2Mutable = [s2 mutableCopy]; for (int i = 0; i 0) { allowedReplaceCount--; } else { return NO; } } } return allowedReplaceCount == 0; } static BOOL oneInsertApart(NSString *s1, NSString *s2) { for (int i = 0; i s2.length) { result = oneInsertApart(s1, s2); } return result; } int main(int argc, const char * argv[]) { @autoreleasepool { // oea("cat", "cut") => true // replace "u" -> "a" // oea("cat", "cuts") => false // no operations // oea("ca", "ca") => false // no operations // oea("cats", "cat") => true // remove "s" // oea("cat", "at") => true // insert "c" // oea("cat", "cbat") => true // remove "b" NSLog(@"%@", oneEditApart(@"cat", @"cut") ? @"true" : @"false"); NSLog(@"%@", oneEditApart(@"cat", @"cuts") ? @"true" : @"false"); NSLog(@"%@", oneEditApart(@"ca", @"ca") ? @"true" : @"false"); NSLog(@"%@", oneEditApart(@"cats", @"cat") ? @"true" : @"false"); NSLog(@"%@", oneEditApart(@"cat", @"at") ? @"true" : @"false"); NSLog(@"%@", oneEditApart(@"cat", @"cbat") ? @"true" : @"false"); } return 0; }