ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง(Swift)

2022. 10. 15. 14:30ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/17677

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 

 ๋‚˜์˜ ํ’€์ด

 

์ฒ˜์Œ์—๋Š” Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ค‘๋ณต๋„ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์„ ์ด์šฉํ–ˆ๋‹ค.

๋จผ์ € allSatisfy๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์ด ์ „๋ถ€ ๋ฌธ์ž์ธ ๊ฒฝ์šฐ์—๋งŒ ๋‹ด์•„์ค€ ๋’ค์— ์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋‘๋ฒˆ์งธ ๋ฐฐ์—ด์— ๊ฐ’์ด ๋‹ด๊ฒจ์žˆ๋‹ค๋ฉด ์ œ๊ฑฐ, ์•„๋‹ˆ๋ฉด ๋„˜์–ด๊ฐ€๋Š” ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์ œ์˜ ๋‚š์‹œํฌ์ธํŠธ(?)๋Š” ์ง‘ํ•ฉ A์™€ ์ง‘ํ•ฉ B๊ฐ€ ๊ณต์ง‘ํ•ฉ์ผ ๋•Œ๋Š” 1์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ์ด๋Š” ๊ต์ง‘ํ•ฉ๋„ 0์ด๊ณ  ํ•ฉ์ง‘ํ•ฉ๋„ 0์ธ ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•ด์„œ ํ’€์–ด์ค˜์•ผํ•œ๋‹ค.

 

import Foundation
func solution(_ str1:String, _ str2:String) -> Int {
    var resultArr1: [String] = []
    var resultArr2: [String] = []
    
    var filteredArr1 = Array(str1.uppercased()).map{ String($0)}
    var filteredArr2 = Array(str2.uppercased()).map{ String($0)}
    var interCount = 0
    
    for (i, val) in filteredArr1.enumerated() {
        if i == filteredArr1.count-1 {
            break
        } 
        let compositionStr = val + filteredArr1[i+1] 
        if compositionStr.allSatisfy({ $0.isLetter }){
            resultArr1.append(compositionStr)
        }
    }
    
    for (i, val) in filteredArr2.enumerated() {
        if i == filteredArr2.count-1 {
            break
        } 
        let compositionStr = val + filteredArr2[i+1] 
        
        if compositionStr.allSatisfy({ $0.isLetter }){
            resultArr2.append(compositionStr)
        }
    }
    
    for x in resultArr1 {
        if resultArr2.contains(x) {
            resultArr1.remove(at: resultArr1.firstIndex(of: x)!)
            resultArr2.remove(at: resultArr2.firstIndex(of: x)!)
            interCount += 1
        } 
    }
    let sum = interCount + resultArr1.count + resultArr2.count
    
    return sum == 0 ? 65536 : Int((Double(interCount)  / Double(sum)) * 65536)
}