Formatted question description: https://leetcode.ca/all/423.html
423. Reconstruct Original Digits from English
Level
Medium
Description
Given a nonempty string containing an outoforder English representation of digits 09
, 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”
Solution
The number of occurrences for each letter does not change no matter how the order of letters changes, so count the number of occurrences for each letter in the given string.
Some words have letters that do not occur in other words, including “z” in “zero”, “w” in “two”, “u” in “four, “x” in “six”, and “g” in “eight”. Therefore, if one of the letters occur in the string, then the corresponding digit must occur. For each of the digits, find the maximum possible occurrences of the digit, which is determined by the letter only occuring in the digit’s word, append the digit to the result string, and reduce the number of occurrences of the letters accordingly.
After 0, 2, 4, 6, and 8 are figured out, there are other letters that occur in only one word, including “o” in “one”, “h”, “r” and “t” in “three”, “f” in “five” and “s” in “seven”. Do the steps above accordingly and append the digits to the result string.
After 1, 3, 5, 7 are figured out, there are only letters for digit 9, so count the number of occurrences of digit 9 using the remaining letters, and append the digit 9 to the result string according to the number of occurrences.
Finally, sort the result string and return.

class Solution { public String originalDigits(String s) { String[] wordsArray = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int[] numsInOrder = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9}; int[] counts = new int[26]; int length = s.length(); for (int i = 0; i < length; i++) counts[s.charAt(i)  'a']++; StringBuffer sb = new StringBuffer(); for (int i = 0; i < 10; i++) { int num = numsInOrder[i]; String word = wordsArray[num]; int[] curCounts = new int[26]; int wordLength = word.length(); for (int j = 0; j < wordLength; j++) curCounts[word.charAt(j)  'a']++; int digitCount = Integer.MAX_VALUE; for (int j = 0; j < 26; j++) { if (curCounts[j] > 0) digitCount = Math.min(digitCount, counts[j] / curCounts[j]); } for (int j = 0; j < digitCount; j++) sb.append(num); for (int j = 0; j < 26; j++) { if (curCounts[j] > 0) counts[j] = curCounts[j] * digitCount; } } char[] digitsArray = sb.toString().toCharArray(); Arrays.sort(digitsArray); return new String(digitsArray); } }

Todo

nums = {0: "zero", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten"} feature = {0: "z", 1: "o", 2: "w", 3: "r", 4: "u", 5: "v", 6: "x", 7: "s", 8: "g", 9: "i", 10: "t"} class Solution(object): def originalDigits(self, s): """ :type s: str :rtype: str """ global nums, feature ans = [] count = {} for c in s: count[c] = count.get(c, 0) + 1 for num in [0, 2, 4, 6, 8, 1, 3, 7, 5, 10, 9]: featureNum = count.get(feature[num], 0) if featureNum > 0: ans += [str(num)] * featureNum word = nums[num] for c in word: count[c] = featureNum ans.sort() return "".join(ans)