diff --git "a/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_[1\354\260\250] \353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/JSON.swift" "b/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_[1\354\260\250] \353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/JSON.swift" new file mode 100644 index 0000000..dde8b7d --- /dev/null +++ "b/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_[1\354\260\250] \353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/JSON.swift" @@ -0,0 +1,37 @@ +func solution(_ str1:String, _ str2:String) -> Int { + var result = 65536 + let (lhs, rhs) = (parse(str1), parse(str2)) + + let intersection = getIntersection(lhs, rhs) + let union = lhs.values.reduce(0, +) + rhs.values.reduce(0, +) - intersection + + guard union != 0 else { return 65536 } + + result = Int(Double(intersection) / Double(union) * 65536) + + return result +} + +func getIntersection(_ lhs: [String: Int], _ rhs: [String: Int]) -> Int { + var result = 0 + + for word in lhs.keys { + result += min(lhs[word, default: 0], rhs[word, default: 0]) + } + + return result +} + +func parse(_ str: String) -> [String: Int] { + var result = [String: Int]() + let arr = Array(str) + + for i in 0..