[프로그래머스] 숫자 짝꿍(Kotlin)💫

0

프로그래머스

목록 보기
100/128
post-thumbnail
post-custom-banner

[프로그래머스] 숫자 짝꿍(Kotlin)

틀린 풀이1

import kotlin.math.*

class Solution {
    fun solution(X: String, Y: String): String {
        var xDigits = Array(10, {it->0})
        var yDigits = Array(10, {it->0})
        for(x in X) xDigits[x.code - '0'.code]++
        for(y in Y) yDigits[y.code - '0'.code]++
        
        var answer = ""
        for(i in 9 downTo 0){
            for(j in 0 until min(xDigits[i], yDigits[i])) {
                answer += ('0'.code + i).toChar()
            }
        }
       
        if(answer == "") return "-1"
        if(answer[0] == '0') return "0"
        return answer
    }
}

틀린 풀이2

import kotlin.math.*

class Solution {
    fun solution(X: String, Y: String): String {
        var xList = X.toCharArray().sorted().reversed()
        var yList = Y.toCharArray().sorted().reversed()
        
        var answer = ""
        var xIndex = 0
        var yIndex = 0
        while((xIndex < xList.size) && (yIndex < yList.size)){
            if(xList[xIndex] == yList[yIndex]){
                answer += xList[xIndex].toString()
                xIndex++
                yIndex++
            }
            else if(xList[xIndex] > yList[yIndex]) xIndex++
            else if(xList[xIndex] < yList[yIndex]) yIndex++
        }
       
        if(answer == "") return "-1"
        if(answer[0] == '0') return "0"
        return answer
    }
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글