423. Reconstruct Original Digits from English
Last updated
Last updated
/**
* Time complexity : O(N)
* Space complexity : O(1)
*/
class Solution {
public String originalDigits(String s) {
int sLen = s.length();
int[] count = new int[10];
for(int i = 0; i < sLen; i++) {
char ch = s.charAt(i);
if(ch == 'z') count[0]++;
else if(ch == 'o') count[1]++;
else if(ch == 'w') count[2]++;
else if(ch == 'h') count[3]++;
else if(ch == 'u') count[4]++;
else if(ch == 'f') count[5]++;
else if(ch == 'x') count[6]++;
else if(ch == 's') count[7]++;
else if(ch == 'g') count[8]++;
else if(ch == 'i') count[9]++;
}
count[1] -= (count[0] + count[2] + count[4]);
count[3] -= count[8];
count[5] -= count[4];
count[7] -= count[6];
count[9] -= (count[5] + count[6] + count[8]);
StringBuilder resultSB = new StringBuilder();
for(int i = 0; i < count.length; i++) {
while(count[i]-- > 0) {
resultSB.append(i);
}
}
return resultSB.toString();
}
}