Pregunta de entrevista de Meta

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"

Respuestas de entrevistas

Anónimo

8 sept 2014

#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; }

Anónimo

16 sept 2014

def oneEditApart(one, two) apartCount = 0 if two.length > one.length two.split('').zip(one.split('')).each do |slice| apartCount += 1 unless slice[0] == slice[1] end else one.split('').zip(two.split('')).each do |slice| apartCount += 1 unless slice[0] == slice[1] end end return apartCount == 1 end

Anónimo

16 sept 2014

def oneEditApart(one, two) return apart(one.split(''), two.split('')) == 1 end def apart(one, two) if one.length == 0 or two.length == 0 return [one.length, two.length].max elsif one[0] == two[0] return apart(one[1..-1], two[1..-1]) else return [apart(one, two[1..-1]), apart(one[1..-1], two), apart(one[1..-1], two[1..-1])].min + 1 end end

1