import Foundation

func solution(_ str1:String, _ str2:String) -> Int {

    let alphabet = "abcdefghijklmnopqrstuvwxyz"

    let lowerStr1 = str1.lowercased().map { $0 }
    let lowerStr2 = str2.lowercased().map { $0 }

    var set1 = [String]()
    var set2 = [String]()

    var union = [String]()
    var intersection = [String]()

    for i in 0..<lowerStr1.count-1 {
        let ch1 = lowerStr1[i]
        let ch2 = lowerStr1[i+1]

        if alphabet.contains(ch1), alphabet.contains(ch2) {
            set1.append("\(ch1)\(ch2)")
        }
    }


    for i in 0..<lowerStr2.count-1 {
        let ch1 = lowerStr2[i]
        let ch2 = lowerStr2[i+1]

        if alphabet.contains(ch1), alphabet.contains(ch2) {
            let str = "\(ch1)\(ch2)"
            set2.append(str)
            if set1.contains(str) {
                intersection.append(str)
                set1.remove(at: set1.firstIndex(of: str)!)
                set2.remove(at: set2.firstIndex(of: str)!)
            }
        }
    }

    union = set1 + set2 + intersection
    
    return intersection.isEmpty && union.isEmpty ? 65536 :Int(Double(intersection.count)/Double(union.count)*65536)
}

'프로그래머스 > Lv.2' 카테고리의 다른 글

[Swift] 멀리 뛰기  (0) 2023.02.04
[Swift] 다음 큰 숫자  (0) 2023.02.04
[Swift] 기능개발  (0) 2023.02.04
[Swift] 괄호 회전하기  (0) 2023.02.04
[Swift] 가장 큰 수  (0) 2023.02.04

+ Recent posts