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(_ str1: String, _ str2: String) -> Bool {
return str1.lowercased().sorted() == str2.lowercased().sorted()- 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
- }
import XCTest // XCTest Spec Example: // TODO: replace with your own tests (TDD), these are just how-to examples to get you started class SolutionTest: XCTestCase { static var allTests = [ ("Test Example", testExample), ] func testExample() { XCTAssertTrue(areMetaStrings("geeks", "keegs")) XCTAssertFalse(areMetaStrings("rsting", "string")) XCTAssertFalse(areMetaStrings("geeks", "peegs")) } } XCTMain([ testCase(SolutionTest.allTests) ])
- import XCTest
- // XCTest Spec Example:
- // TODO: replace with your own tests (TDD), these are just how-to examples to get you started
- class SolutionTest: XCTestCase {
- static var allTests = [
- ("Test Example", testExample),
- ]
- func testExample() {
- XCTAssertTrue(areMetaStrings("geeks", "keegs"))
XCTAssertTrue(areMetaStrings("rsting", "string"))- XCTAssertFalse(areMetaStrings("rsting", "string"))
- XCTAssertFalse(areMetaStrings("geeks", "peegs"))
- }
- }
- XCTMain([
- testCase(SolutionTest.allTests)
- ])