์ ๋‹นํ•œ ๊ณ ํ†ต์€ ํฌ์—ด์ด๋‹ค

- ๋Œ„ ๋ธŒ๋ผ์šด '๋‹ค๋นˆ์น˜ ์ฝ”๋“œ' ์ค‘์—์„œ

Algorithm/Programmers

[Swift ์•Œ๊ณ ๋ฆฌ์ฆ˜] Programmers ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

hongssup_ 2022. 11. 10. 23:57
๋ฐ˜์‘ํ˜•

โ–ณ

Level 2 2018 KAKAO BLIND RECRUITMENT (3์‹œ๊ฐ„?๐Ÿ˜…)

[1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

๋ฌธ์ œ ํ’€๋ฉด์„œ๋„ ๊ณ„์† ํ—ท๊ฐˆ๋ ค์„œ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.. ์œผ ! 

์ง„์งœ ์นด์นด์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ๋”์ฐํ•˜๊ฒŒ ๊ธธ๊ณ  ๋ณต์žกํ•œ๋“ฏ.. ๐Ÿคข ๋ฌธ์ œ ๊ผผ๊ผผํ•˜๊ฒŒ ์ž˜์ฝ์ž!

๋ฌธ์ œ ์„ค๋ช…
์œ ์‚ฌํ•œ ๊ธฐ์‚ฌ๋ฅผ ๋ฌถ๋Š” ๊ธฐ์ค€์„ ์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋…ผ๋ฌธ๊ณผ ์ž๋ฃŒ๋ฅผ ์กฐ์‚ฌํ•˜๋˜ ํŠœ๋ธŒ๋Š” "์ž์นด๋“œ ์œ ์‚ฌ๋„"๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ƒˆ๋‹ค.
์ž์นด๋“œ ์œ ์‚ฌ๋„๋Š” ์ง‘ํ•ฉ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ• ์ค‘์˜ ํ•˜๋‚˜๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ๋‘ ์ง‘ํ•ฉ A, B ์‚ฌ์ด์˜ ์ž์นด๋“œ ์œ ์‚ฌ๋„ J(A, B)๋Š” ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋ฅผ ๋‘ ์ง‘ํ•ฉ์˜ ํ•ฉ์ง‘ํ•ฉ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์œผ๋กœ ์ •์˜๋œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์ง‘ํ•ฉ A = {1, 2, 3}, ์ง‘ํ•ฉ B = {2, 3, 4}๋ผ๊ณ  ํ•  ๋•Œ, ๊ต์ง‘ํ•ฉ A ∩ B = {2, 3}, ํ•ฉ์ง‘ํ•ฉ A ∪ B = {1, 2, 3, 4}์ด ๋˜๋ฏ€๋กœ, ์ง‘ํ•ฉ A, B ์‚ฌ์ด์˜ ์ž์นด๋“œ ์œ ์‚ฌ๋„ J(A, B) = 2/4 = 0.5๊ฐ€ ๋œ๋‹ค. ์ง‘ํ•ฉ A์™€ ์ง‘ํ•ฉ B๊ฐ€ ๋ชจ๋‘ ๊ณต์ง‘ํ•ฉ์ผ ๊ฒฝ์šฐ์—๋Š” ๋‚˜๋ˆ—์…ˆ์ด ์ •์˜๋˜์ง€ ์•Š์œผ๋‹ˆ ๋”ฐ๋กœ J(A, B) = 1๋กœ ์ •์˜ํ•œ๋‹ค.
์ž์นด๋“œ ์œ ์‚ฌ๋„๋Š” ์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋‹ค์ค‘์ง‘ํ•ฉ์— ๋Œ€ํ•ด์„œ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์ค‘์ง‘ํ•ฉ A๋Š” ์›์†Œ "1"์„ 3๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๋‹ค์ค‘์ง‘ํ•ฉ B๋Š” ์›์†Œ "1"์„ 5๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ A ∩ B๋Š” ์›์†Œ "1"์„ min(3, 5)์ธ 3๊ฐœ, ํ•ฉ์ง‘ํ•ฉ A ∪ B๋Š” ์›์†Œ "1"์„ max(3, 5)์ธ 5๊ฐœ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋‹ค์ค‘์ง‘ํ•ฉ A = {1, 1, 2, 2, 3}, ๋‹ค์ค‘์ง‘ํ•ฉ B = {1, 2, 2, 4, 5}๋ผ๊ณ  ํ•˜๋ฉด, ๊ต์ง‘ํ•ฉ A ∩ B = {1, 2, 2}, ํ•ฉ์ง‘ํ•ฉ A ∪ B = {1, 1, 2, 2, 3, 4, 5}๊ฐ€ ๋˜๋ฏ€๋กœ, ์ž์นด๋“œ ์œ ์‚ฌ๋„ J(A, B) = 3/7, ์•ฝ 0.42๊ฐ€ ๋œ๋‹ค.
์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š”๋ฐ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด "FRANCE"์™€ "FRENCH"๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋‹ค์ค‘์ง‘ํ•ฉ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ {FR, RA, AN, NC, CE}, {FR, RE, EN, NC, CH}๊ฐ€ ๋˜๋ฉฐ, ๊ต์ง‘ํ•ฉ์€ {FR, NC}, ํ•ฉ์ง‘ํ•ฉ์€ {FR, RA, AN, NC, CE, RE, EN, CH}๊ฐ€ ๋˜๋ฏ€๋กœ, ๋‘ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ์ž์นด๋“œ ์œ ์‚ฌ๋„ J("FRANCE", "FRENCH") = 2/8 = 0.25๊ฐ€ ๋œ๋‹ค.
์ž…๋ ฅ ํ˜•์‹
- ์ž…๋ ฅ์œผ๋กœ๋Š” str1๊ณผ str2์˜ ๋‘ ๋ฌธ์ž์—ด์ด ๋“ค์–ด์˜จ๋‹ค. ๊ฐ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ, 1,000 ์ดํ•˜์ด๋‹ค.
- ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๋ฌธ์ž์—ด์€ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ์›์†Œ๋กœ ๋งŒ๋“ ๋‹ค. ์ด๋•Œ ์˜๋ฌธ์ž๋กœ ๋œ ๊ธ€์ž ์Œ๋งŒ ์œ ํšจํ•˜๊ณ , ๊ธฐํƒ€ ๊ณต๋ฐฑ์ด๋‚˜ ์ˆซ์ž, ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๊ทธ ๊ธ€์ž ์Œ์„ ๋ฒ„๋ฆฐ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "ab+"๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด, "ab"๋งŒ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ์›์†Œ๋กœ ์‚ผ๊ณ , "b+"๋Š” ๋ฒ„๋ฆฐ๋‹ค.
- ๋‹ค์ค‘์ง‘ํ•ฉ ์›์†Œ ์‚ฌ์ด๋ฅผ ๋น„๊ตํ•  ๋•Œ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž์˜ ์ฐจ์ด๋Š” ๋ฌด์‹œํ•œ๋‹ค. "AB"์™€ "Ab", "ab"๋Š” ๊ฐ™์€ ์›์†Œ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
์ถœ๋ ฅ ํ˜•์‹
์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๋‘ ๋ฌธ์ž์—ด์˜ ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์œ ์‚ฌ๋„ ๊ฐ’์€ 0์—์„œ 1 ์‚ฌ์ด์˜ ์‹ค์ˆ˜์ด๋ฏ€๋กœ, ์ด๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์‰ฝ๋„๋ก 65536์„ ๊ณฑํ•œ ํ›„์— ์†Œ์ˆ˜์  ์•„๋ž˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ์ •์ˆ˜๋ถ€๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

print(solution("FRANCE", "french")) //16384
print(solution("handshake", "shake hands")) //16384
print(solution("aa1+aa2", "AAAA12")) //43690
print(solution("E=M*C^2", "e=m*c^2")) //65536

 

๋‚ด ๋‹ต์•ˆ

func solution(_ str1:String, _ str2:String) -> Int {
    //๋‹ค์ค‘์ง‘ํ•ฉ ์›์†Œ ๋งŒ๋“ค๊ธฐ
    func letterSet(_ str: String) -> [String] {
        var str = str.lowercased()
        var arr = [String]()
        while str.count > 1 {
            var letter = ""
            for i in str.prefix(2) {
                if i.isLetter {
                    letter += String(i)
                }
            }
            if letter.count == 2 {
                arr.append(letter)
            }
            str = String(str.dropFirst())
        }
        return arr
    }
    
    let arr1 = letterSet(str1)
    let arr2 = letterSet(str2)
    
    if arr1.count == 0 && arr2.count == 0 { return 65536 }
    
    //๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    var intersection = 0
    let inter = arr1.filter{ arr2.contains($0) }
    for i in Set(inter) {
        let count1 = arr1.filter{ $0 == i }.count
        let count2 = arr2.filter{ $0 == i }.count
        intersection += min(count1, count2)
    }
    //ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
    let union = arr1.count + arr2.count - intersection
    
    let result = 65536 * Double(intersection) / Double(union)
    return Int(result)
}

ํ’€์ด๊ณผ์ •

์ค‘๋ณต ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์ด ๊นŒ๋‹ค๋กœ์› ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์ค‘์ง‘ํ•ฉ ์ƒ์„ฑ ์‹œ ์ธ๋ฑ์‹ฑ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค์–ด๋ณผ๊นŒ ์ƒ๊ฐ๋„ ํ–ˆ๋Š”๋ฐ, ๊ตฌํ˜„์€ ๊ทธ๊ฒŒ ๋” ๋ณต์žกํ•  ๊ฒƒ ๊ฐ™์•„์„œ ํ•ฉ/๊ต์ง‘ํ•ฉ ๋งŒ๋“ค๋•Œ ์ฒ˜๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ๋•๋ถ„์— ๊ต์ง‘ํ•ฉ ๋งŒ๋“ค๋•Œ ๊ณ„์† ํ—ท๊ฐˆ๋ ค์„œ ์• ๋จน์—ˆ๋‹ค ใ…Ž 

1. ๋‹ค์ค‘์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

 ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ์ง์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋‹ค์ค‘์ง‘ํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค

  1) ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜

  2) ์•ž๊ธ€์ž ๋‘ ๊ฐœ๊ฐ€ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋ฉด ๋‹ค์ค‘์ง‘ํ•ฉ ์›์†Œ๋กœ ์ถ”๊ฐ€

  3) ๋ฌธ์ž์—ด ๋งจ ์•ž๊ธ€์ž ์ œ๊ฑฐ 

2. ๊ต์ง‘ํ•ฉ / ํ•ฉ์ง‘ํ•ฉ ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๊ธฐ

์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋‹ค์ค‘์ง‘ํ•ฉ์ด๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•ด์•ผ ํ•œ๋‹ค! ์ด๊ฑฐ ๋•Œ๋ฌธ์— ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์„œ ์‹œ๊ฐ„ ๋งŽ์ด ๋‚ ๋ ธ๋‹ค..

  1) ๋‘ ๋‹ค์ค‘์ง‘ํ•ฉ์—์„œ ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ์›์†Œ ๋ฐฐ์—ด inter ์ƒ์„ฑ (์›์†Œ ์ค‘๋ณต ๊ฐ€๋Šฅ)

  2) ์›์†Œ ์ค‘๋ณต์„ ์—†์•ค Set(inter)๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค, ๋‘ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ์›์†Œ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•ด์ค€ ํ›„ ๋” ์ž‘์€ ์ˆ˜๋ฅผ ๊ต์ง‘ํ•ฉ ๊ฐฏ์ˆ˜์— ๋”ํ•ด์ค€๋‹ค. 

  3) ํ•ฉ์ง‘ํ•ฉ = ์ง‘ํ•ฉA + ์ง‘ํ•ฉB - A๊ต์ง‘ํ•ฉ

๋”๋ณด๊ธฐ

์ฒ˜์Œ์— ๋ฌธ์ œ ์ดํ•ด๋ฅผ ์ž˜๋ชปํ•ด์„œ ์ข€ ํ—ค๋งธ๋‹ค. 

์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋‹ค์ค‘์ง‘ํ•ฉ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ฐ„๊ณผํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ฉ์ง‘ํ•ฉ ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•ด์ฃผ์—ˆ๋”๋‹ˆ ๋‹น์—ฐํžˆ ๋‹ต์ด ์•ˆ๋‚˜์˜ค๋”๋ผ. 

//ํ•ฉ์ง‘ํ•ฉ
let union = Set(arr1 + arr2).count
//๊ต์ง‘ํ•ฉ
let intersection = arr1.filter{ arr2.contains($0) }.count

3. ๊ฒฐ๊ณผ๊ฐ’ ๊ณ„์‚ฐ ๋ฐ ๋ฐ˜ํ™˜

 

 

๋ณด์™„ ๋‹ต์•ˆ

๋‚ด ์ฝ”๋“œ ๋„ˆ๋ฌด ๊ธธ๊ณ  ๋น„ํšจ์œจ์ ์ธ๊ฐ€ ๊ฑฑ์ •ํ–ˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ๋ถ„๋“ค ์ฝ”๋“œ๋„ ๋งŒ๋งŒ์น˜ ์•Š๋”๋ผ. ์›Œ๋‚™ ๋ณต์žกํ•˜๊ณ  ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ๋ผ.. 

๋‹ค์ค‘์ง‘ํ•ฉ ์›์†Œ ๋งŒ๋“ค ๋•Œ, ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด๋กœ ๋งคํ•‘ํ•ด์ค€ ํ›„ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋‹ˆ if๋ฌธ์„ ์ค„์ผ ์ˆ˜ ์žˆ์–ด ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค.

๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•  ๋•Œ์—๋„ filter๋กœ ๊ต์ง‘ํ•ฉ ์›์†Œ ๊ตฌํ•ด์„œ Set ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ์ง€ ์•Š์•„๋„, Set์—์„œ ์ง€์›๋˜๋Š” intersection ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.  

//๋‹ค์ค‘์ง‘ํ•ฉ ์›์†Œ ๋งŒ๋“ค๊ธฐ
func letterSet(_ str: String) -> [String] {
    let str = str.map{ $0.lowercased() }
    var arr = [String]()
    for i in 0..<str.count - 1 {
        if Character(str[i]).isLetter && Character(str[i+1]).isLetter {
            arr.append(str[i] + str[i+1])
        }
    }
    return arr
}

let arr1 = letterSet(str1)
let arr2 = letterSet(str2)

//๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
var intersection = 0
for i in Set(arr1).intersection(Set(arr2)) {
    let count1 = arr1.filter{ $0 == i }.count
    let count2 = arr2.filter{ $0 == i }.count
    intersection += min(count1, count2)
}
728x90
๋ฐ˜์‘ํ˜•