[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ถˆ๋Ÿ‰ ์‚ฌ์šฉ์ž

2021. 6. 9. 15:08ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift - Algorithm - Solutions


๐Ÿค” ํ’€์ด ๊ณผ์ •

user_id์™€ banned_id๊ฐ€ String ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง€๊ฒŒ๋ฉ๋‹ˆ๋‹ค. banned_id๋ฅผ ๊ธฐ์ค€์œผ๋กœ user_id๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋งค์นญ๋˜๋Š” id๋“ค์˜ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌถ์–ด์„œ ์ €์žฅํ•ด์ค€๋’ค ๋งค์นญ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ผ€์ด์Šค์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ์กฐํ•ฉ์€ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๊ตฌํ•ด๋ณด์•˜์œผ๋ฉฐ ํ•˜๋‚˜์˜ banned_id์™€ ๋งค์นญ๋˜๋Š” user_id์˜ ์ธ๋ฑ์Šค๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค Set์— ์ €์žฅํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

 


๐Ÿ“ฆ  ๋ฌธ์ œ ํ’€์ด & ์ฝ”๋“œ

import Foundation

func solution(_ user_id: [String], _ banned_id: [String]) -> Int {

    // ์ธ๋ฑ์Šค๋ผ๋ฆฌ ๋งค์นญ, ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” banned_id์˜ ์ธ๋ฑ์Šค,๊ฐ’์€ ๋งค์นญ๋˜๋Š” user_id๋“ค์˜ ์ธ๋ฑ์Šค ๋ฐฐ์—ด
    var matchUpWithIndex = [[Int]](repeating: [Int](), count: banned_id.count)
    
    // Set์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ผ€์ด์Šค๋“ค์„ ์ €์žฅ
    var matchUpCases = Set<[Int]>()
    
    // "*" ๋ฌธ์ž๋ฅผ ์ฒดํฌํ•˜๋ฉฐ ๋งค์นญํ•ด๋ณด๋Š” ํ•จ์ˆ˜
    func matching(_ user_id: String, _ banned_id: String) -> Bool {
        if user_id.count != banned_id.count {
            return false
        }
        
        let tempUser = user_id.map{$0}
        let tempBanned = banned_id.map{$0}
        
        for idx in 0..<tempUser.count {
            if tempUser[idx] != tempBanned[idx] && tempBanned[idx] != "*" {
                return false
            }
        }
        
        return true
    }
    
    // ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์กฐํ•ฉ๋“ค์„ ๊ตฌํ•˜์—ฌ matchUpCases์— ์ €์žฅ.
    func combination(_ curBannedIdx: Int, selected: [Int]) {
        if curBannedIdx == banned_id.count {
        
            // ex) 0๋ฒˆ์งธ banned_id์™€ [3, 2, 1] ๋งค์นญ == [1, 2, 3] ๋งค์นญ
            // ๋งค์นญ๋˜๋Š” user ์ธ๋ฑ์Šค๋“ค์˜ ์ˆœ์„œ๋Š” ์ •๋ ฌํ•˜์—ฌ ๊ฐ™์€๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ
            
            matchUpCases.insert(selected.sorted(by: <))
            
        } else {
            var temp = selected
            
            for userIdx in matchUpWithIndex[curBannedIdx] {
                if temp.contains(userIdx) == false {
                    temp.append(userIdx)
                    combination(curBannedIdx + 1, selected: temp)
                    temp.removeLast()
                }
            }
        }
    }
    
    for bannedIndex in 0..<banned_id.count {
        for userIndex in 0..<user_id.count {
            if matching(user_id[userIndex], banned_id[bannedIndex]) {
                matchUpWithIndex[bannedIndex].append(userIndex)
            }
        }
    }
    
    combination(0, selected: [])
    
    return matchUpCases.count
}
 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ถˆ๋Ÿ‰ ์‚ฌ์šฉ์ž

๊ฐœ๋ฐœํŒ€ ๋‚ด์—์„œ ์ด๋ฒคํŠธ ๊ฐœ๋ฐœ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š” "๋ฌด์ง€"๋Š” ์ตœ๊ทผ ์ง„ํ–‰๋œ ์นด์นด์˜ค์ด๋ชจํ‹ฐ์ฝ˜ ์ด๋ฒคํŠธ์— ๋น„์ •์ƒ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹น์ฒจ์„ ์‹œ๋„ํ•œ ์‘๋ชจ์ž๋“ค์„ ๋ฐœ๊ฒฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‘๋ชจ์ž๋“ค์„ ๋”ฐ๋กœ ๋ชจ์•„ ๋ถˆ๋Ÿ‰

programmers.co.kr