LeetCode: Reconstruct Original Digits From English Solution
1/**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)2223 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 }4445 return frequencies46 .map((frequency, digit) => String(digit).repeat(frequency))47 .filter(Boolean)48 .join("")49}
Comments
Loading comments...
Tags
leetcode
math
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.