423. Reconstruct Original Digits from English
Description
Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.
Example 1:
Input: s = "owoztneoer" Output: "012"
Example 2:
Input: s = "fviefuro" Output: "45"
Constraints:
1 <= s.length <= 105s[i]is one of the characters["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"].sis guaranteed to be valid.
Solutions
Solution 1
Thinking
English digit words overlap, so matching words in order is unsafe. A few letters belong to only one digit.
\(\texttt{z}\) is unique to zero, \(\texttt{w}\) to two, \(\texttt{u}\) to four, \(\texttt{x}\) to six, \(\texttt{g}\) to eight. After subtracting those, \(\texttt{h}\), \(\texttt{f}\), \(\texttt{s}\) give three, five, seven, and finally \(\texttt{o}\) and \(\textit{i}\) give one and nine.
Counting along that unique-letter order solves a triangular system; concatenate digits from \(0\) to \(9\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |