# Ruby solution with explanation

• ``````# @param {String} s
# @return {String}
def original_digits(s)
# count occurence of each character is `s`
char_counts = s.each_char.with_object(Hash.new(0)) do |char, hash|
hash[char] += 1
end

digits = Array.new(10, 0)

digits[0] = poll(char_counts, 'zero',  'z')  # 'z' only appears in 'zero'
digits[2] = poll(char_counts, 'two',   'w')  # 'w' only appears in 'two'
digits[4] = poll(char_counts, 'four',  'u')  # 'u' only appears in 'four'
digits[6] = poll(char_counts, 'six',   'x')  # 'x' only appears in 'six'
digits[8] = poll(char_counts, 'eight', 'g')  # 'g' only appears in 'eight'
digits[7] = poll(char_counts, 'seven', 's')  # since 'six'es are all handled, 's' only appears in 'seven'
digits[5] = poll(char_counts, 'five',  'v')  # since 'seven's are all handled, 'v' only appears in 'five'
digits[1] = poll(char_counts, 'one',   'o')  # since 'zero'es and 'four's are all handled, 'o' only appears in 'one'
digits[9] = poll(char_counts, 'nine',  'i')  # we don't pick 'n' as character of 'nine' because 'nine' has 2 'n's
digits[3] = poll(char_counts, 'three', 't')  # actually the character doesn't matter now

digits.each_with_index.with_object('') do |(count, digit), s|
s << (digit.to_s * count)
end
end

def poll(char_counts, word, character)
count = char_counts[character]
word.each_char {|char| char_counts[char] -= count}
count
end
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.