# LeetCode: Reconstruct Original Digits From English Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {string} s3 * @return {string}4 */5var originalDigits = function (s) {6  const counter = s7    .split("")8    .reduce((acc, c) => acc.set(c, (acc.get(c) || 0) + 1), new Map())9  const digitLiterals = [10    "zero",11    "one",12    "two",13    "three",14    "four",15    "five",16    "six",17    "seven",18    "eight",19    "nine",20  ]21  const frequencies = Array(10).fill(0)22
23  for (const [uniqueChar, digit] of [24    ["z", 0],25    ["w", 2],26    ["u", 4],27    ["x", 6],28    ["g", 8],29    ["s", 7],30    ["f", 5],31    ["o", 1],32    ["h", 3],33    ["i", 9],34  ]) {35    const frequency = counter.get(uniqueChar) || 036    frequencies[digit] += frequency37    for (const char of digitLiterals[digit]) {38      counter.set(char, (counter.get(char) || 0) - frequency)39      if (counter.get(char) && counter.get(char) <= 0) {40        counter.delete(char)41      }42    }43  }44
45  return frequencies46    .map((frequency, digit) => String(digit).repeat(frequency))47    .filter(Boolean)48    .join("")49}```

leetcode

math

## Next Post

LeetCode: Flip Binary Tree To Match Preorder Traversal

Mar 31, 2021

Search Posts