{"ScriptPreparationCode":"function bestListPosition(word) {\r\n var indexer = {}; // D:3 B:1 A:0 C:2\r\n var counts = []; // 2 1 1 1\r\n\r\n var lettersCount = 0;\r\n word.split(\u0022\u0022).sort().forEach(function(x){\r\n if ( indexer[x] == undefined ) {\r\n indexer[x] = lettersCount;\r\n counts[lettersCount] = 0;\r\n lettersCount \u002B\u002B;\r\n }\r\n });\r\n\r\n var term = 1;\r\n var sum = term;\r\n word.split(\u0022\u0022).reverse().forEach(function(x, i){\r\n var step = i \u002B 1, idx = indexer[x];\r\n counts[idx] \u002B\u002B;\r\n term /= counts[idx];\r\n for (var j = 0; j \u003C idx; \u002B\u002Bj) \r\n if (counts[j] != 0) \r\n sum \u002B= term * counts[j];\r\n term *= step;\r\n });\r\n return sum;\r\n}\r\n\r\nfunction myListPosition(word) {\r\n console.log(word);\r\n var array = word.split(\u0027\u0027);\r\n var sorted = array.slice().sort();\r\n return word_helper(array, sorted);\r\n}\r\n\r\nfunction word_helper(array, sorted){\r\n if (array.length === 0) return 1;\r\n \r\n var first = array[0];\r\n \r\n if (first === sorted[0]){\r\n return word_helper(array.slice(1), sorted.slice(1));\r\n }\r\n \r\n var done=[];\r\n var num = 0;\r\n for (let i = 0; sorted[i] !== first; i\u002B\u002B){\r\n if (done.length \u0026\u0026 done[done.length - 1] === sorted[i])\r\n continue;\r\n done.push(sorted[i]);\r\n \r\n num \u002B= permutations(remove(array, sorted[i]), remove(sorted, sorted[i])); \r\n }\r\n \r\n return num \u002B word_helper(array.slice(1), remove(sorted, first));\r\n}\r\n\r\nfunction remove(array, element){\r\n return array.slice(0, array.indexOf(element))\r\n .concat(array.slice(array.indexOf(element) \u002B 1));\r\n}\r\n\r\nfunction permutations(array, sorted){\r\n var dups = [];\r\n current = 0;\r\n for (let i = 1; i \u003C sorted.length; i\u002B\u002B){\r\n if (sorted[i] === sorted[i-1]){\r\n current \u002B= current ? 1 : 2;\r\n } else if (current){\r\n dups.push(current);\r\n current = 0;\r\n }\r\n }\r\n if (current) dups.push(current);\r\n return factorial(array.length) / dups.reduce((p,c) =\u003E p * factorial(c), 1);\r\n}\r\n\r\nfunction factorial(n){\r\n var ret = 1;\r\n for (; n \u003E 0; n--){\r\n ret *= n;\r\n }\r\n return ret;\r\n}\r\n\r\ntest_words=\u0027thing|qergqergqebqergqerghwervqeg|wergqeruihoiuehfoiuh|gelrjflibqhf|greigubwreiugbqerg|qqergwrgqergfwrrth|bwnfofbbwoobgbjhwfhbbbhwofbbwofbgbw\u0027.split(\u0027\u0027);","TestCases":[{"Name":"my solution","Code":"for(word of test_words){\r\n myListPosition(word);\r\n}","IsDeferred":false},{"Name":"best solution","Code":"for (word of test_words){\r\n bestListPosition(word);\r\n}","IsDeferred":false}]}