Я правильно понял?
function oms_zakra(el) {
function s(a) {
return a.reduce(function (a, b) {
return a + b
}, 0)
}
function c(a) {
return a[0].reduce(function (e, b, g) {
e[g] = [];
for (b = 0; b < a.length; b++) e[g].push(a[b][g]);
return e
}, {})
}
function fn(a, e) {
return Object.keys(a).sort(function (b, g) {
return e ? s(a[g]) - s(a[b]) : s(a[b]) - s(a[g])
})[0]
}
function r(a, e) {
return e.reduce(function (b, e, h) {
e && (b[h] = a[h]);
return b
}, {})
}
function ff(a, e) {
return a.map(function (a) {
return a.filter(function (a, b) {
return 0 == e[b]
})
})
}
function print(a, e, b) {
a = a.reduce(function (a, b) {
return a + "<div>" + JSON.stringify(b) + "</div>"
}, e);
document.body.insertAdjacentHTML("beforeEnd", a + b)
}
var m = el,
temp = "абвгдежзи".split("");
temp.length = m.length;
for (var res = []; 2 < m.length;) {
print(temp, "", "");
print(m, "", "");
var d = c(m),
n = fn(d),
t = d[n],
f = r(m, t),
k = fn(f, !0);
res.push(temp.splice(k, 1)[0]);
var v = m.splice(k, 1)[0];
m = ff(m, v)
}
print([res.sort()], "<b>", "</b>");
}