Given a non-empty string containing an out-of-order 中文版 representation of digits 0-9
, output the digits in ascending Order.
Note:
- Input contains only lowercase 中文版 letters.
- Input is guaranteed to being valid and can be transformed to its original digits. That means invalid inputs such as "ABC" or "Zerone" is not permitted.
- Input length is less than 50,000.
Example 1:
Input: "Owoztneoer" Output: "012"
Example 2:
Input: "Fviefuro" Output: "45"
This problem gives us a string of English strings, which are made up of English words that represent numbers, but the order of characters is scrambled, let's reconstruct the numbers. Then the idea of the problem is to count the number of occurrences of each character, and then calculate the number of occurrences of each word, and then you can rebuild. Since the input string must be valid in the title, there will be no successful rebuild, and a trick is required here. We look closely at the words "zero", "one", "Double", "three", "four", "five", "six", "seven", "eight", "nine", and we can find that the characters of some words are unique, such as Z, Only appear in zero, there are w,u,x,g these four words, respectively, only appear in Two,four,six,eight, then the number of these five numbers can be determined, because the word containing O has zero,two,four,one, of which the first three are determined, Then the number of one will know, because the word containing H has eight,three, of which the number of eight is known, then the number of three know; because the word containing F has four,five, of which the number of four is known, then the number of five will know Because the word containing S has six,seven, of which the number of six is known, then the number of seven will be known, because the words containing I have six,eight,five,nine, of which the first three are determined, then the number of nine will know, It is easier to know these problems, we are in this order "zero", "one", "four", "six", "eight", "one", "three", "five", "seven", "nine" can find all the number, see the code is as follows:
Solution One:
classSolution { Public: stringOriginaldigits (strings) {stringres =""; Vector<string> words{"Zero"," Both"," Four","Six","Eight"," One","three","Five","Seven","Nine"}; Vector<int> nums{0,2,4,6,8,1,3,5,7,9}, Counts ( -,0); Vector<Char> chars{'Z','W','u','x','g','o','h','F','s','I'}; for(Charc:s) ++counts[c-'a']; for(inti =0; I <Ten; ++i) {intCNT = Counts[chars[i]-'a']; for(intj =0; J < Words[i].size (); ++j) {Counts[words[i][j]-'a'] -=CNT; } while(cnt--) Res + = (Nums[i] +'0'); } sort (Res.begin (), Res.end ()); returnRes; }};
In addition, we can use a more concise method to quickly find out the number of numbers, see the code is as follows:
Solution Two:
classSolution { Public: stringOriginaldigits (strings) {stringres =""; Vector<int> Counts ( -,0), Nums (Ten,0); for(CharC:S) + +Counts[c]; nums[0] = counts['Z']; nums[2] = counts['W']; nums[4] = counts['u']; nums[6] = counts['x']; nums[8] = counts['g']; nums[1] = counts['o']-nums[0]-nums[2]-nums[4]; nums[3] = counts['h']-nums[8]; nums[5] = counts['F']-nums[4]; nums[7] = counts['s']-nums[6]; nums[9] = counts['I']-nums[6]-nums[8]-nums[5]; for(inti =0; I < nums.size (); ++i) { for(intj =0; J < Nums[i]; ++j) {Res+ = (i +'0'); } } returnRes; }};
Resources:
Https://discuss.leetcode.com/topic/64150/straightforward-c-accepted-solution
Https://discuss.leetcode.com/topic/63382/share-my-simple-and-easy-o-n-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Reconstruct Original Digits from 中文版 rebuilding numbers in English