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 30 31 32 33 34 35 36 37 38
| var originalDigits = function(s) { const eng = [ { index: 0, en: 'zero' }, { index: 6, en: 'six' }, { index: 8, en: 'eight' }, { index: 2, en: 'two' }, { index: 3, en: 'three' }, { index: 7, en: 'seven' }, { index: 5, en: 'five' }, { index: 9, en: 'nine' }, { index: 1, en: 'one' }, { index: 4, en: 'four' } ] const tag = ['z', 'n', 'w', 'h', 'u', 'v', 'x', 's', 'g', 'i'] const mp = new Map() for (const ch of s) { mp.set(ch, (mp.get(ch) || 0) + 1) } const arr = new Array(10).fill(0) for (const { index, en } of eng) { if (mp.has(tag[index])) { arr[index] = mp.get(tag[index]) for (const ch of en) { mp.set(ch, mp.get(ch) - arr[index]) } } } let res = '' for (let i = 0; i < 10; i++) { for (let j = 0; j < arr[i]; j++) { res += i } } return res };
|