https://school.programmers.co.kr/learn/courses/30/lessons/131128
테스트 11~15 실패(시간초과)
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
}
}
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
}
}