Given a non-empty string containing an out-of-order English representation of digits 0-9
, output the digits in ascending order.
Note:
- Input contains only lowercase English letters.
- Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
- Input length is less than 50,000.
Example 1:
Input: "owoztneoer" Output: "012"
Example 2:
Input: "fviefuro" Output: "45"
统计字符频数,然后从英文数字的字符差异求出每个数的频数
public class Solution {
public String originalDigits(String s) {
int[] alpha = new int[256];
int[] ten = new int[10];
for(int i=0; i<s.length(); i++)
alpha[s.charAt(i)]++;
ten[0] = alpha['z'];
ten[2] = alpha['w'];
ten[6] = alpha['x'];
ten[8] = alpha['g'];
ten[3] = alpha['h']-ten[8];
ten[4] = alpha['r']-ten[0]-ten[3];
ten[5] = alpha['f']-ten[4];
ten[7] = alpha['v']-ten[5];
ten[1] = alpha['o']-ten[0]-ten[2]-ten[4];
ten[9] = alpha['i']-ten[5]-ten[6]-ten[8];
StringBuilder sb = new StringBuilder();
for(int i=0; i<10; i++){
while(ten[i]>0){
sb.append(""+i);
ten[i]--;
}
}
return sb.toString();
}
}