๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด #81301

by ํ‚ค์œค 2023. 11. 29.

#81301

๐ŸŽ„ Question ?

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

class Solution {
    fun solution(s: String): Int {
        var answer: Int = 0
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. ๊ฐ€์žฅ ๋จผ์ € string์—์„œ string์˜ ์ผ๋ถ€๋ถ„(substring์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค. ์ฐพ์•„๋ณด๋‹ˆ subSequence(startIndex, endIndex) ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜์—ฌ substring์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค. endIndex๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฑธ ์‘์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.
  2. ๊ฐ€์žฅ ๋จผ์ € s์˜ character๋ฅผ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๊ณ (์•„์Šคํ‚ค ์ฝ”๋“œ ์‘์šฉํ•ด์„œ ํ’€์ดํ• ๊ฑฐ์ž„) ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  3. while~do๋ฌธ์œผ๋กœ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ ๋งŒํผ ๋Œ๋ ค์คŒ. (size ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉํ•˜๊ธฐ)
  4. ๊ทธ ์•ˆ์— when ๋ฌธ์„ ๋žŒ๋‹ค์‹์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•ด์ฃผ๊ธฐ. 
  5. ์˜๋ฌธ์œผ๋กœ ์ ์€ ์ˆซ์ž๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์€ z, o, t, f, s, e, n ์ด ์ผ๊ณฑ๊ฐœ์ด๊ณ  ์ด ์•ŒํŒŒ๋ฒณ์— ๋Œ€์‘ํ•˜๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด์ฃผ๊ธฐ
  6. ์‹œ์ž‘๋˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฒฝ์šฐ ๋‚˜๋ˆ„์–ด์ฃผ๊ธฐ
class Solution {
    fun solution(s: String): Int {
        var answer: List<Int> = 0
        var sArray = s.toCharArray()
        var i = 0
        do {
            when(sArray[i]) {
                in "z" -> (i += 4
                    answer.add(0))
                in "o" -> (i += 3
                    answer.add(1))
                in "t" -> (
                    when (sArray[i+1]) {
                        in "w" -> (i += 3
                            answer.add(2))
                        else -> (i += 5
                            answer.add(3))
                        })
                in "f" -> (
                    when (sArray[i+1]) {
                        in "o" -> (i += 4
                            answer.add(4))
                        else -> (i += 4
                            answer.add(5))
                        })
                in "s" -> (
                    when (sArray[i+1]) {
                        in "i" -> (i += 3
                            answer.add(6))
                        else -> (i += 5
                            answer.add(7))
                        })
                in "e" -> (i += 5
                    answer.add(8))
                in "n" -> (i += 4
                    answer.add(9))
                else -> (answer.add(sArray[i].digitToInt())
                    i++
                )
            }
        } while(i < sArray.size)

        return answer.toInt()
    }
}

์—‰๋ง์ด์ง€๋งŒ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•ด๋ณธ ์ฝ”๋“œ.

๐ŸŽ€ Answer

package Kakao_Internship_2021.์ˆซ์ž_๋ฌธ์ž์—ด๊ณผ_์˜๋‹จ์–ด

class Solution {
    fun solution(s: String): Int {
        val nums = arrayOf("zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine")
        var str = s
        for (i in 0 until nums.size) {
            str = str.replace(nums[i], i.toString())
        }
        return str.toInt()
    }
}

nums๋ผ๋Š” array์— ๋ชจ๋“  ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ์ €์žฅ๋œ ์ˆ˜์˜ ์ธ๋ฑ์Šค๊ฐ€ ์ ํ˜€์žˆ๋Š” ์ˆ˜์™€ ์ผ์น˜ํ•จ์„ ์ด์šฉํ•˜์˜€๋‹ค.
ํ’€์ด ๋ฏธ์ณค๋‹ค..

ํ’€์ด 2

fun main(args: Array<String>) {
    println(solution("one4seveneight"))
    println(solution("23four5six7"))
    println(solution("2three45sixseven"))
    println(solution("123"))
}

fun solution(s: String): Int {
    val dicNumber: HashMap<String, String>
    dicNumber = hashMapOf(
        "one" to "1",
        "two" to "2",
        "three" to "3",
        "four" to "4",
        "five" to "5",
        "six" to "6",
        "seven" to "7",
        "eight" to "8",
        "nine" to "9",
        "zero" to "0"
    )
    var answer = ""
    var stringNumber: String = ""
    for (str in s) {
        if (str.isDigit()) answer += str
        else {
            stringNumber += str
        }
        if (dicNumber.containsKey(stringNumber)) {
            answer += dicNumber[stringNumber]
            stringNumber = ""
        }
    }
    return answer.toInt()
}

๋”•์…”๋„ˆ๋ฆฌ๋ž‘ hashmapof ์‚ฌ์šฉํ•œ ์ฝ”๋“œ.

๐ŸŽ Result

 

๐Ÿ† Comment

์‹ฑ๊ธฐ...