-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path숫자 짝꿍.java
25 lines (20 loc) · 833 Bytes
/
숫자 짝꿍.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
class Solution {
public String solution(String X, String Y) {
StringBuilder sb = new StringBuilder();
int[] xcount = new int[10];
int[] ycount = new int[10];
for(int i=0; i<X.length(); i++) xcount[X.charAt(i)-'0']++;
for(int i=0; i<Y.length(); i++) ycount[Y.charAt(i)-'0']++;
ArrayList<Integer> ans = new ArrayList<>();
for(int i=9; i>=0; i--){
if(Math.min(xcount[i],ycount[i]) > 0){
for(int j=0; j<Math.min(xcount[i],ycount[i]); j++) ans.add(i);
}
}
for(int i =0; i<ans.size();i++) sb.append(ans.get(i));
if (sb.toString().equals("")) return "-1";
else if (sb.toString().startsWith("0")) return "0";
else return sb.toString();
}
}