SW Test/Programmers

[Programmers] Lv1.์ˆซ์ž ์ง๊ฟ(Kotlin)

An effort will never betray ๐Ÿ˜Ž 2023. 10. 15. 16:39
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

 

์˜ˆ์‹œ

 

 

ํ’€์ด

  • X์˜ ํฌํ•จ๋œ ๋ฌธ์ž๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  Y์— ํฌํ•จ๋œ ๋ฌธ์ž๊ฐ€ X์˜ ์นด์šดํŒ… ๋ฐฐ์—ด์—์„œ 0 ์ด์ƒ์ธ ๊ฒฝ์šฐ์— ๊ณตํ†ต๋œ ์ˆซ์ž๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด์— ๊ฐ’์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • ์ดํ›„ ํฌ๊ธฐ๊ฐ€ ํฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
class Solution {
    fun solution(X: String, Y: String): String {
        var answer: String = ""
        val counts = MutableList(10){0}
        val nums = mutableListOf<Int>()
        
        X.forEach{ 
            counts[it.toString().toInt()]++
        }
        
        Y.forEach {
            if(counts[it.toString().toInt()] > 0){
                nums.add(it.toString().toInt())
                counts[it.toString().toInt()]--
            }
        }
        
        return if(nums.isEmpty()) "-1"
        else if(nums.sum() == 0) "0"
        else nums.sortedDescending().joinToString("")
    }
}

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
๋ฐ˜์‘ํ˜•