a year ago

### Almost one liner with n * log(n) time.

``````const solve = (strOne, strTwo) => {
const cache = Object.fromEntries([...strTwo].map((c, i) => [c, i+1]));
return strOne.split('').sort((a,b) => (cache[a] ?? Number.MAX_SAFE_INTEGER) - (cache[b] ?? Number.MAX_SAFE_INTEGER)).join('')
};
``````

### Unfortunately this editor doesn't support `??` and `Object.fromEntries` So had to change to older syntax:

``````const solve = (strOne, strTwo) => {
const cache = {};
[...strTwo].map((c, i) => cache[c] = i+1);
return strOne.split('').sort((a,b) => (cache[a] || Number.MAX_SAFE_INTEGER) - (cache[b] || Number.MAX_SAFE_INTEGER)).join('')
};
``````

a year ago

``````const solve = (strOne, strTwo) => {

const order = {};
for (let i = 0; i < strTwo.length; i++) {
order[strTwo[i]] = 0;
}

for (let i = 0; i < strOne.length; i++) {
order[strOne[i]] = order[strOne[i]]+1 || 1;
}

let output = '';
for (let character in order) {
output += character.repeat(order[character]);
}
return output;
};
``````

2 years ago

const solve = (strOne, strTwo) => { let op = []; for (let i = 0; i < strTwo.length; i++) { for (let j = 0; j < strOne.length; j++) { if (strOne[j] === strTwo[i]) { op.push(strOne[j]); } } } if (op.length !== strOne.length) { for (let val of strOne) { if (!strTwo.includes(val)) { op.push(val); } } } return op.join(""); };

2 years ago

``````const solve = (strOne, strTwo) => {
let notFoundString = [...strOne].filter(el => ![...strTwo].includes(el)).join('');
let result = [...strTwo].filter(el => [...strOne].includes(el)).map(el => strOne.match(new RegExp(el,'g'))).flat().join('');
return result + notFoundString
};
``````

3 years ago

`const solve = (strOne, strTwo) => { var final = []; var arrOne = strOne.split(''); var arrTwo = strTwo.split(''); for(var i = 0; i < arrTwo.length; i++){ arrOne.forEach((value, index) => { if(value == arrTwo[i]){ final.push(arrOne.splice(index, 1)) } }) } final.push(arrOne); var last = final.flat().join('') return last; };`