Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - [3์ฐจ]์••์ถ•

๊ฐ์ž ๐Ÿฅ” 2022. 9. 14. 01:26
๋ฐ˜์‘ํ˜•

๐ŸŸ  TIL (ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ ๋ฐฐ์šด์ )

โค๏ธ‍๐Ÿ”ฅ swift ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฐพ๊ธฐ (๋”•์…”๋„ˆ๋ฆฌ contain ์‚ฌ์šฉ๋ฒ•)

๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ ๊ฐ€์ง„ key ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” contain์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.ํ•˜์ง€๋งŒ, ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์—์„œ๋Š” 'ํด๋กœ์ €'๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค.

func wcCheckInDict(_ w: String, _ c: String, dict: [String:Int]) -> Bool {
    let wc = w+c
    let isinDict = dict.contains { $0.key == wc }
    return isinDict
}

์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ, dict์—์„œ contain์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , key๋ƒ value๋ƒ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋œ๋‹ค.๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Bool๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค!

 

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ 

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

 

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

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

programmers.co.kr

 

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

์‹คํŒจํ•œ ๋ฐฉ๋ฒ•

  1. w์™€ c๋ฅผ ์•ž๋ถ€ํ„ฐ ์„ ํƒ
  2. input์—์„œ w๋ฅผ ์—†์• ์คŒ (input๋ฐฐ์—ด์—๋Š” ํ˜„์žฌ ์••์ถ•ํ•˜๋ ค๋Š” ๋ฌธ์ž๋Š” ์—†๊ณ , ๋‹ค์Œ ๋ฌธ์ž๋ถ€ํ„ฐ ๋‹ด๊ฒจ์žˆ๊ฒŒ ๋œ๋‹ค.)
  3. w+c ๊ฐ€ dict์— ์žˆ๋Š”์ง€ ํ™•์ธ
  4. ์—†๋‹ค๋ฉด w์˜ value๋ฅผ answer์— ์ถ”๊ฐ€ / w+c๋ฅผ dict์— ์ถ”๊ฐ€
  5. ์žˆ๋‹ค๋ฉด ๋‹ค์‹œ ๋ฐ˜๋ณตํ•ด์„œ w+c+c+c,,,,๊ฐ€ dict๋‚ด๋ถ€์— ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ๋ฐ˜๋ณต
  6. ๊ทธ๋ž˜๋„ ์—†๋‹ค๋ฉด w๋ฅผ answer์— ์ถ”๊ฐ€ / w+c๋ฅผ dict์— ์ถ”๊ฐ€

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ, input์˜ ๊ธธ์ด๊ฐ€ 0์ผ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋˜๊ณ , ์ด๋Ÿด ๊ฒฝ์šฐ์— ์ˆ˜์—†์ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋กœ if ๋ฌธ์„ ์ž…๋ ฅํ•ด์•ผํ–ˆ๋‹ค. ์™œ๋ƒ๋ฉด ๋‚˜๋Š” removeFirst ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒŒ์†ํ•ด์„œ input ๋‚ด๋ถ€์— ์žˆ๋Š” value๋“ค์„ ๋บด๋‚ด๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๊ณผ์ •์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์™„์ „ํžˆ... ๊ผฌ์ด๊ฒŒ ๋˜๋Š”๋ฐ..... 

(์ •๋‹ต ์ฝ”๋“œ์˜ ํ’€์ด ๋ฐฉ๋ฒ•์€ ๋ฐ‘์— ์ ์–ด๋†จ์Šต๋‹ˆ๋‹ค)

 

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ํ’€์ด (์‹คํŒจ) - ์ฝ”๋“œ ์ ‘์–ด๋‘ 

์•„๋ž˜ ์ ‘์€ ๊ธ€์—์„œ ์‹คํŒจํ•œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋”๋ณด๊ธฐ

์‹คํŒจ ์ฝ”๋“œ

import Foundation

func solution(_ msg:String) -> [Int] {

    // 1. dictionary ์ƒ์„ฑ
    var dict = ["A": 1, "B": 2, "C": 3, "D": 4, "E": 5,
                "F": 6, "G": 7, "H": 8, "I": 9, "J": 10,
                "K": 11, "L": 12, "M": 13, "N": 14, "O": 15,
                "P": 16, "Q": 17, "R": 18, "S": 19, "T": 20,
                "U": 21, "V": 22, "W": 23, "X": 24, "Y": 25,
                "Z": 26]
    var valueNum = 27
    var input = msg.map { String($0) }
    var w = input[0]
    var c = input[1]
    input.removeFirst()
    var answer = [Int]()

    while input.count >= 0 {
        // w+c๊ฐ€ dict์— ์žˆ์œผ๋ฉด wc ๊ฐฑ์‹ ํ•ด์ฃผ๊ณ , ๋‹ค์Œ์œผ๋กœ ๋„˜๊ฒจ์•ผํ•จ
        let isinDict = wcCheckInDict(w, c, dict: dict)
        if isinDict {
            w = w+c
            input.removeFirst()
            if input.count == 0 {
                continue
            }
            c = input[0]
            continue
        } else if input.count == 0 { // ๋งˆ์ง€๋ง‰ ์ธ๊ฒฝ์šฐ
            answer.append(dict[w]!)
            break
        } else { // ์—†์œผ๋ฉด w+c๋ฅผ dict์— ๋„ฃ์–ด์คŒ ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— w์™€ c๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•จ
            dict[w+c] = valueNum
            valueNum += 1

            answer.append(dict[w]!)

            w = input.removeFirst()
            if input.count == 0 {
                continue
            }
            c = input[0]
        }
    }
    return answer
}


func wcCheckInDict(_ w: String, _ c: String, dict: [String:Int]) -> Bool {
    let wc = w+c
    let isinDict = dict.contains { $0.key == wc }
    return isinDict
}

 

์ด๋ ‡๊ฒŒ ์ด ๋‹ค์„ฏ ๊ฐœ์˜ case ์—์„œ ์‹คํŒจ๋ฅผ ํ–ˆ๋‹ค. "signal: illegal instruction (core dumped)"๊ฐ€ ๋ญ˜๊นŒ?
์ด๊ฑด ์ธํ„ฐ๋„ท ์„œ์นญ ๊ฒฐ๊ณผ, 'index out of range' ์—๋Ÿฌ์ด๊ฑฐ๋‚˜ ๋„์ค‘์— nil ๊ฐ’์ด ์ƒ์„ฑ๋์„๋•Œ ์ƒ๊ธฐ๋Š” ์—๋Ÿฌ๋ผ๊ณ  ํ•œ๋‹ค.

์–ด๋–ค ๋ถ€๋ถ„์—์„œ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ฒผ๋Š”์ง€ ๊ณ ๋ฏผํ•ด๋ด์•ผํ–ˆ๋‹ค. 

๐ŸŸ  ๋‘๋ฒˆ์งธ ํ’€์ด (ํ‹€๋ฆผ) - ์ฝ”๋“œ ์ ‘์–ด๋‘ 

๋”๋ณด๊ธฐ

์‹คํŒจํ•œ ์ฝ”๋“œ

import Foundation

func solution(_ msg:String) -> [Int] {

    // 1. dictionary ์ƒ์„ฑ
    var dict = ["A": 1, "B": 2, "C": 3, "D": 4, "E": 5,
                "F": 6, "G": 7, "H": 8, "I": 9, "J": 10,
                "K": 11, "L": 12, "M": 13, "N": 14, "O": 15,
                "P": 16, "Q": 17, "R": 18, "S": 19, "T": 20,
                "U": 21, "V": 22, "W": 23, "X": 24, "Y": 25,
                "Z": 26]
    var valueNum = 27
    var input = msg.map { String($0) }
    var w = input[0]
    var c = input[1]
    input.removeFirst()
    var answer = [Int]()

    while input.count >= 0 {
        // w+c๊ฐ€ dict์— ์žˆ์œผ๋ฉด wc ๊ฐฑ์‹ ํ•ด์ฃผ๊ณ , ๋‹ค์Œ์œผ๋กœ ๋„˜๊ฒจ์•ผํ•จ
        let isinDict = wcCheckInDict(w, c, dict: dict)
        if isinDict {
            w = w+c
            input.removeFirst()
            if input.count == 0 {
                continue
            }
            c = input[0]
            continue
        } else if input.count == 0 { // ๋งˆ์ง€๋ง‰ ์ธ๊ฒฝ์šฐ
            answer.append(dict[w]!)
            break
        } else { // ์—†์œผ๋ฉด w+c๋ฅผ dict์— ๋„ฃ์–ด์คŒ ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— w์™€ c๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•จ
            dict[w+c] = valueNum
            valueNum += 1

            answer.append(dict[w]!)
            w = input.removeFirst()
            if input.count == 0 {
                c = "9999"
                continue
            }
            c = input[0]
        }
    }
    return answer
}


func wcCheckInDict(_ w: String, _ c: String, dict: [String:Int]) -> Bool {
    let wc = w+c
    let isinDict = dict.contains { $0.key == wc }
    return isinDict
}

16๋ฒˆ ํ•˜๋‚˜๋ฅผ ๋งž์•˜๋‹ค.. ์•„ ... ํ•œ๊ฐœ๋ผ๋„ ์–ด๋””์•ผ

์›์ธ์€ ์•„๋ž˜์™€ ๊ฐ™์•˜๋‹ค.

else { // ์—†์œผ๋ฉด w+c๋ฅผ dict์— ๋„ฃ์–ด์คŒ ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— w์™€ c๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•จ
            dict[w+c] = valueNum
            valueNum += 1

            answer.append(dict[w]!)
            w = input.removeFirst()
            if input.count == 0 {
                c = "9999"
                continue
            }
            c = input[0]
        }

ํ‹€๋ฆฐ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ฌ๊ธฐ์„œ c = "9999" ๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋งŒ์•ฝ w์—๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋ฐฐ์ •ํ•˜๊ณ , ๋‚จ์€ input์ด ์—†์„ ๊ฒฝ์šฐ, c๋ฅผ ๊ธฐ์กด์˜ ๊ฐ’์œผ๋ก ๊ทธ๋ƒฅ ๋‘๊ฒŒ ๋œ๋‹ค๋ฉด, ์šฐ์—ฐ์˜ ์ผ์น˜๋กœ  ๋งจ ์œ„์—์„œ ๊ฒ€ํ† ํ•  ๋•Œ w+c๊ฐ’์ด ๋˜! dict์— ์กด์žฌํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋‹ˆ๊นŒ 

  • input = ["A"]
  • w = "A" ๋กœ ๋ฐฐ์ •๋ฐ›์Œ (removefirst๋ฅผ ํ†ตํ•ด์„œ)
  • ๊ธฐ์กด์˜ c ๋Š” ๋งŒ์•ฝ Z์˜€์Œ
  • ๊ทธ๋Ÿผ, ๋‹ค์‹œ while๋ฌธ ํ†ต๊ณผ๋ฅผ ์œ„ํ•ด ๋งจ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ”์„ ๊ฒฝ์šฐ, w+c = "AZ"๊ฐ€ ๋˜๊ณ , A์— ๋Œ€ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”๋ฐ AZ์— ๋Œ€ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋จ. ๊ทธ๋ž˜์„œ ์•„์˜ˆ ์ธ์‹์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ˆซ์ž ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.

 

๐ŸŸ  ์„ธ๋ฒˆ์งธ ํ’€์ด (์ •๋‹ต์ฝ”๋“œ) 

https://github.com/deslog/Algorithm/tree/main/Algorithm/Programmers

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

์•„,,, ์œ„์—์„œ ์™œ ์‹ค์ˆ˜ํ–ˆ๋‚˜ ์ง„์งœ ์ง€๊ธˆ ์„ธ์‹œ๊ฐ„์ •๋„ ์”จ๋ฆ„ํ•œ๊ฒƒ ๊ฐ™๋‹ค. ์ •๋ง ์งˆ๋ฆฌ๊ณ  ์งˆ๋ฆฌ๊ณ  ์งˆ๋ฆฐ๋‹ค!!!!

๊ฒฐ๊ตญ, ํŠน์ • case ์—์„œ out of index ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜, nil๊ฐ’์ด ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜, ์–ด์จ‹๋“  index๊ฐ€ ๋ฌธ์ œ์ธ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์™œ๋ƒ๋ฉด, ์œ„์— ๋‘๋ฒˆ์งธ ํ’€์ด์—์„œ ๋ณด๋ฉด, if๋ฌธ์„ ํ†ตํ•ด์„œ ์ผ๋ถ€๋กœ w์™€ c๊ฐ’์„ ๊ณ„์† ์กฐ์ •ํ•ด์ฃผ๊ณ , input์—์„œ removeFIrst๋ฅผ ํ•ด์ฃผ๋ฉด์„œ, ๋งŒ์•ฝ input์ด ๋น„์—ˆ์œผ๋ฉด~ c์—๋‹ค๊ฐ€๋Š” ์ด๋ ‡๊ฒŒ ๋‘๊ณ ,,,,, ์ •๋ง ๋ฐ”๋ณด๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ณ„์† input์ด ๋น„์–ด์žˆ๋Š” ์ƒํƒœ์—์„œ removefirst๋ฅผ ํ•˜๊ฒŒ ๋˜์–ด index ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋˜ ๊ฒƒ์ด๋‹ค... 

๊ทธ๋ž˜์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ƒ๊ฐํ–ˆ๋‹ค.

์šฐ์„ , ๋˜ ๋‚ด๊ฐ€ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ์ด ์žˆ์—ˆ๋‹ค. ๋ฐ”๋กœ , ์••์ถ•ํ• ๋ ค๋Š” ๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋œ ๋ฌธ์ž๋ณด๋‹ค ์ปค์ง€๋Š” ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋‹ค. ์••์ถ•ํ•ด์„œ dict์— ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์‚ฌ์‹ค input์—์„œ ๋“ค์–ด์˜จ ๊ธธ์ด๋ณด๋‹ค ํฌ๋ฉด, ์˜๋ฏธ๊ฐ€ ์—†๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ์ฒ˜๋ฆฌ๋„ ๋”ฐ๋กœ ํ•„์š”ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ while๋ฌธ์„ ๋Œ๋ฆด ๋•Œ, input์˜ ๊ธธ์ด๋งŒํผ ๋Œ๋ฆฌ๊ธฐ๋กœ ํ–ˆ๋”ฐ. ์ด๋Š” count๋กœ ์กฐ์ •ํ–ˆ๊ณ , while๋ฌธ์„ ํ•œ๋ฒˆ ๋Œ๋–„๋งˆ๋‹ค count์— 1์„ ๋”ํ•ด์ค€๋‹ค. ์ฆ‰ ์ž…๋ ฅ์ด "KAKAO"์ด๋ฉด, count๊ฐ€ 5 ์ดํ•˜์—ฌ์•ผ ๋Œ์•„๊ฐ„๋‹ค, ์ฆ‰, Kํ•œ๋ฒˆ๋Œ๊ณ  A ๋Œ๊ณ , K,, A,, ์ด๋ ‡๊ฒŒ ๋„ค๋ฒˆ ๋Œ๊ณ  while๋ฌธ์„ ํƒˆ์ถœํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งจ ๋งˆ์ง€๋ง‰ O๋Š” while๋ฌธ์„ ํƒˆ์ถœํ•˜๊ณ  ํ•œ๋ฒˆ ๋” append ๋กœ anser์— ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์ •๋‹ต..... ํ•˜... ์ง„์ž‘์— KAKAO ๋ผ๋Š” ์ž…๋ ฅ์ด๋ฉด, ์ด ๊ธธ์ด๋งŒํผ ๋Œ๋ฆฌ๋ฉด๋˜๊ฒŸ๊ตฌ๋‚˜! ๋ฅผ ์ƒ๊ฐํ–ˆ์–ด์•ผ ๋นจ๋ฆฌ ํ’€๋ ธ์„ ๊ฒƒ ๊ฐ™๋‹ค ใ…  ์™œ input์„ ๋ง‰ ๋„ฃ์—ˆ๋‹ค๊ฐ€ ๋บ๋‹ค๊ฐ€ ํ–ˆ์„๊นŒ ใ… 

func solution(_ msg:String) -> [Int] {
    var dict = ["A": 1, "B": 2, "C": 3, "D": 4, "E": 5,
                "F": 6, "G": 7, "H": 8, "I": 9, "J": 10,
                "K": 11, "L": 12, "M": 13, "N": 14, "O": 15,
                "P": 16, "Q": 17, "R": 18, "S": 19, "T": 20,
                "U": 21, "V": 22, "W": 23, "X": 24, "Y": 25,
                "Z": 26]
    let input = msg.map { String($0) }
    var answer = [Int]()
    var count = 0 // count ํ•ด๊ฐ€๋ฉด์„œ while๋ฌธ์„ ๋Œ๋ฆด ๊ฑฐ์ž„
    var w = "" // str์—๋‹ค๊ฐ€ ๋”ํ•ด๊ฐ€๋ฉด์„œ ์ง„ํ–‰
    while count < input.count {
        let c = input[count] // ๋‹ค์Œ๊ธ€์ž
        let tempwc = w + c
        let isinDict = wcCheckInDict(w, c, dict: dict)

        if !isinDict { // dict์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
            dict[tempwc] = dict.count + 1 // ์ผ๋‹จ dict ์— w+c ์ถ”๊ฐ€
            answer.append(dict[w]!) // ๊ทธ๋ฆฌ๊ณ  answer์—๋Š” w value ์ถœ๋ ฅ
            w = ""
            continue
        }

        w += c
        count += 1
    }
    answer.append(dict[w]!)
    return answer
}

func wcCheckInDict(_ w: String, _ c: String, dict: [String:Int]) -> Bool {
    let wc = w+c
    let isinDict = dict.contains { $0.key == wc }
    return isinDict
}

// ํ…Œ์ŠคํŠธ ๋Œ๋ ค๋ณด๊ธฐ
let msg = "TOBEORNOTTOBEORTOBEORNOT"
// ๊ฒฐ๊ณผ [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]
let a = solution(msg)
print(a)

 

โค๏ธ‍๐Ÿ”ฅ ๋‹ค๋ฅธ์‚ฌ๋žŒ์ฝ”๋“œ์—์„œ ๋ฐฐ์› ๋˜ ์ !!

๋‚˜๋Š” dict์„ ๊ตฌ์„ฑํ• ๋–„ A๋ถ€ํ„ฐ Z ๊นŒ์ง€ ์ˆ˜๊ธฐ๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.์ƒ๋‹นํžˆ ๊ท€์ฐฎ์•˜๋‹ค.. ๊ทผ๋ฐ ์ด๊ฒƒ์„ ์šฐ๋ฆฌ ์Šคํ„ฐ๋””์› ์ค‘ ํ•œ ๋ถ„์ด Unicode๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ€ํ˜•ํ•˜๊ณ , ์ด๋ฅผ for๋ฌธ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋“ฑ๋กํ•˜์…จ๋˜ ๊ฒƒ์ด๋‹ค.. ์ฒœ์žฌ๋‹ค.. ๋งค๋ฒˆ ๋ฌธ์ž์™€ ์ˆซ์ž์‚ฌ์ด์—์„œ ๋ณ€ํ˜•ํ• ๋•Œ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐœ๋…์„ ๊นŒ๋จน๋Š”๋ฐ ๊ผญ ๊ธฐ์–ตํ•˜๊ณ  ๋‚˜์ค‘์— ์จ๋จน์–ด๋ณด์ž.

for i in (1...26) {
    dic[String(UnicodeScalar(i + 64)!)] = i
}

 

๋ฐ˜์‘ํ˜•