小米笔试第二题(号码重排)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
for(int i=0;i<count;++i){
String str = scan.next();
int[] refer = new int[26];
for(int j=0;j<str.length();++j){
char temp = str.charAt(j);
int num = temp - 'A';
refer[num] += 1;
}
System.out.println(factory(refer));
}
scan.close();
}
public static String factory(int[] refer){
int[] result = new int[10];
int temp = recount(refer);
String[] arr =
{"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
for(int i=0;i<arr.length;++i){
while(isExist(refer, arr[i])){
int real = i-8;
if(real < 0)
real += 10;
result[real] += 1;
}
}
StringBuffer str = new StringBuffer();
for(int i=0;i<result.length;i++){
int a = result[i];
while(a != 0){
str.append(i);
a--;
}
}
return str.toString();
}
public static boolean isExist(int[] refer,String str){
boolean isExist = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (refer[c-'A'] == 0) {
isExist = isExist&&false;
}
}
if (isExist) {
for(int i=0;i<str.length();++i){
char c = str.charAt(i);
refer[c- 'A']--;
}
}
return isExist;
}
public static int recount(int[] refer){
int temp = 0;
for(int i=0;i<refer.length;++i){
temp += refer[i];
}
return temp;
}
}