Ad
Code
Diff
  • func areMetaStrings(_ first: String, _ second: String) -> Bool {
      guard first.count == second.count else { return false }
      let combinations = zip(first, second).filter { $0.0 != $0.1 }
      guard combinations.count == 2 else { return false }
      
      return combinations
          .map { [$0.0, $0.1] }
          .flatMap { $0 }
          .reduce(into: Set()) { $0.insert($1) }
          .count == 2
    }
    • func areMetaStrings(_ str1: String, _ str2: String) -> Bool {
    • if str1.count != str1.count {
    • return false
    • }
    • let str1Arr = Array(str1)
    • let str2Arr = Array(str2)
    • var set = Set<Character>()
    • var falseCount = 0
    • for i in 0..<str1.count {
    • if str1Arr[i] != str2Arr[i] {
    • falseCount += !set.insert(str1Arr[i]).inserted ? 1 : 0
    • falseCount += !set.insert(str2Arr[i]).inserted ? 1 : 0
    • }
    • }
    • return falseCount == 2
    • func areMetaStrings(_ first: String, _ second: String) -> Bool {
    • guard first.count == second.count else { return false }
    • let combinations = zip(first, second).filter { $0.0 != $0.1 }
    • guard combinations.count == 2 else { return false }
    • return combinations
    • .map { [$0.0, $0.1] }
    • .flatMap { $0 }
    • .reduce(into: Set()) { $0.insert($1) }
    • .count == 2
    • }