给你一个字符串 s
,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9
)。按 升序 返回原始的数字。
示例 1:
输入:s = "owoztneoer" 输出:"012"
示例 2:
输入:s = "fviefuro" 输出:"45"
提示:
1 <= s.length <= 105
s[i]
为["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]
这些字符之一s
保证是一个符合题目要求的字符串
Related Topics
class Solution {
public String originalDigits(String s) {
//0 zero
//1 one
//2 two
//3 three
//4 four
//5 five
//6 six
//7 seven
//8 eight
//9 nine
int[] charChount = new int[26];
for (int i = 0; i < s.length(); i++) {
charChount[s.charAt(i)-'a']++;
}
int[] arr = new int[10];
arr[0] = charChount[25];
arr[2] = charChount[22];
arr[4] = charChount[20];
arr[6] = charChount[23];
arr[8] = charChount[6];
arr[5] = charChount[5] - arr[4];
arr[3] = charChount[7] - arr[8];
arr[7] = charChount[18] - arr[6];
arr[1] = charChount[14] - arr[0] - arr[2] - arr[4];
arr[9] = charChount[8] - arr[5] - arr[6] - arr[8];
char[] chars = new char[s.length()/3];
int idx = -1;
for (int i = 0; i < arr.length; i++) {
while (--arr[i]>=0){
chars[++idx] = (char)('0'+i);
}
}
idx++;
char[] ans = new char[idx];
System.arraycopy(chars,0,ans,0,idx);
return new String(ans);
}
}
发表评论