**Solution**

```
def original_digits(s)
require 'matrix'
a = ('a'..'z').map { |c| "zero one two three four five six seven eight nine".split.map { |w| w.count(c) } }
b = ('a'..'z').map { |c| s.count(c) }
x = Matrix[*a].lup.solve(b)
(0..9).map { |i| i.to_s * x[i] }.join
end
```

**Explanation**

Using Ruby's `Matrix`

class to solve the system of equations. We have one equation for each letter. For example, consider the letter 'n'. If the unknown digits are x_{0} zeros, x_{1} ones, etc, then the letter 'n' appears x_{1} + x_{7} + 2x_{9} times. Each "one" or "seven" contributes one 'n', each "nine" contributes two 'n', and the other digits don't contribute any 'n'. Now for example if the input is `s = "owoztneoer"`

then we have one 'n' and thus we have the equation **x _{1} + x_{7} + 2x_{9} = 1**. Doing this for all 26 letters gives us 26 equations which we can write as a matrix equation

`Ax = b`

.`A`

is the 26×10 matrix with a_{i,j}telling how often the i-th alphabet letter appears in the j-th digit name.`b`

is the vector of size 26 with b_{i}telling how often the i-th alphabet letter appears in the given`s`

.`x`

is the vector of size 10 with x_{i}telling how often the i-th digit is encoded in the given`s`

.

**Alternatives**

Since `A`

is always the same (doesn't depend on `s`

), we can precompute it so it doesn't get computed every time our function is called:

```
require 'matrix'
A = ('a'..'z').map { |c| "zero one two three four five six seven eight nine".split.map { |w| w.count(c) } }
Solve = Matrix[*A].lup.method(:solve)
def original_digits(s)
x = Solve[('a'..'z').map { |c| s.count(c) }]
(0..9).map { |i| i.to_s * x[i] }.join
end
```

Could even make the actual function a one-liner then:

```
def original_digits(s)
Solve[('a'..'z').map { |c| s.count(c) }][0..9].map.with_index { |x, i| i.to_s * x }.join
end
```