프로그래머스_뉴스클러스터링

hankyulee·2021년 11월 16일
0

Swift coding test 준비

목록 보기
40/57

문제 잘못읽고, 문자가아닌것 모두 지워버려서 한시간 넘었다..
isLetter는 character에 쓸 수 있다.

func solution(_ str1:String, _ str2:String) -> Int {
    var newStr1 = ""
    var newStr2 = ""
    newStr1 = str1
    newStr2 = str2
    
    newStr1 = newStr1.lowercased()
    newStr2 = newStr2.lowercased()

    var str1Array = [String]()
    var str2Array = [String]()
    
    str1Array = Array(newStr1).map{String($0)}
    str2Array = Array(newStr2).map{String($0)}

    var twoZiphop1 = [String]()
    for (i,s) in str1Array.enumerated(){
        if i != str1Array.count - 1 {
            let twoS = s + str1Array[i+1]
            if Character(s).isLetter && Character(str1Array[i+1]).isLetter{
                twoZiphop1.append(twoS)
            }
        }
    }
    
    var twoZiphop2 = [String]()
    for (i,s) in str2Array.enumerated(){
        if i != str2Array.count - 1 {
            let twoS = s + str2Array[i+1]
            if Character(s).isLetter && Character(str2Array[i+1]).isLetter{
                twoZiphop2.append(twoS)
            }
        }
    }
    
    if twoZiphop2.count == 0 && twoZiphop1.count == 0 {
        return 65536
    }

    var set1 = Set(twoZiphop1).union(Set(twoZiphop2))
    var result1 = 0
    var result2 = 0
    for n in set1 {
        print("n",n)
        let A = twoZiphop1.filter{$0 == n}.count
        let B = twoZiphop2.filter{$0 == n}.count
        result1 += max(A,B)
        result2 += min(A,B)
        
    }

    return Int(Double(result2)/Double(result1) * 65536)
}

0개의 댓글