JSFiddle - React, Tailwind, and code Playground
by Andrew Gerst
HTML
<body>
<pre id="test"></pre>
</body>
JavaScript
function Expand_It(s, k) {
if(s.length < 2 || k <= 0) return "-1";
// original requirements
// var split_string = s.match(/([a-z]|[0-9]+)/gi);
// modified shit to map any string as a word.
var split_string = s.match(/([^0-9]+|[0-9]+)/gi);
var str_map = {};
for(var i=0; i<split_string.length; i++){
var num = parseInt(split_string[i+1]);
if( isNaN(parseInt(split_string[i])) && num >= 0 ){
if(typeof(str_map[split_string[i]]) == "undefined"){ str_map[split_string[i]]=0; }
str_map[split_string[i]] += num;
}
}
var keysSorted = Object.keys(str_map).sort(function(a,b){return a.localeCompare(b);});
var seek = 0 ;
for(var key of keysSorted){
seek += str_map[key];
if(seek >= k){
return key;
}
}
return "-1";
}
function perf(str, search, res){
var start = performance.now();
var recieved = Expand_It(str, search);
var result = ( recieved === res )
var end = performance.now();
return {input:str, search:search, test:result, expected:res, recieved:recieved, time_ms:(end-start)};
}
console.log("Function Length: " + Expand_It.toString().replace(/[\s\t]2+|[\n]*/g, "").length);
console.log("Results + Performance: ");
var tests = [];
// general tests
tests.push(perf("c010b01a120", 131, "c"));
tests.push(perf("a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10a2b4a5c7d10", 100, "c"));
tests.push(perf("a2b3c2a1", 2, "a"));
tests.push(perf("a2b3c2a1", 5, "b"));
tests.push(perf("a2b3c2a1", 1, "a"));
tests.push(perf("a2b3c2a1", 8, "c"));
tests.push(perf("a2b2c02", 8, "-1"));
tests.push(perf("a5b0100c000b100", 200, "b"));
var long_str_1 =...