예를 들어, X = 3403이고 Y = 13203이라면, X와 Y의 짝꿍은 X와 Y에서 공통으로 나타나는 3, 0, 3으로 만들 수 있는 가장 큰 정수인 330입니다. 다른 예시로 X = 5525이고 Y = 1255이면 X와 Y의 짝꿍은 X와 Y에서 공통으로 나타나는 2, 5, 5로 만들 수 있는 가장 큰 정수인 552입니다(X에는 5가 3개, Y에는 5가 2개 나타나므로 남는 5 한 개는 짝 지을 수 없습니다.)
두 정수 X, Y가 주어졌을 때, X, Y의 짝꿍을 return하는 solution 함수를 완성해주세요.
-> 자세한 내용 보러가기
class Solution {
public String solution(String X, String Y) {
int[] xCnt = new int[10];
int[] yCnt = new int[10];
char[] xChars = X.toCharArray();
char[] yChars = Y.toCharArray();
for (char ch : xChars) {
xCnt[ch - '0']++;
}
for (char ch : yChars) {
yCnt[ch - '0']++;
}
StringBuilder result = new StringBuilder();
for (int idx = 9; idx >= 0; idx--) {
int cnt = Math.min(xCnt[idx], yCnt[idx]);
for (int i = 0; i < cnt; i++) {
result.append(idx);
}
}
if (result.length() == 0) {
return "-1";
}
if(result.charAt(0) == '0'){
return "0";
}
return result.toString();
}
}