function magic(s) {
// using set instead of list because its faster lookups
const vowels = new Set(["a", "e", "i", "o", "u"]);
let result = [];
// using regular lookup instead of (map,reverse) to reduce the complexity -- o(n) times
for (let i = s.length - 1; i >= 0; i--) {
let char = s[i];
let upperChar = vowels.has(char) ? char.toUpperCase() : char;
if (!(i === 0 && !vowels.has(char))) result.push(upperChar);
}
return result.join("-");
}
console.log(magic("hellothere")); // "E-r-E-h-t-O-l-l-E"
ZnVuY3Rpb24gbWFnaWMocykgewogIC8vIHVzaW5nIHNldCBpbnN0ZWFkIG9mIGxpc3QgYmVjYXVzZSBpdHMgZmFzdGVyIGxvb2t1cHMKICBjb25zdCB2b3dlbHMgPSBuZXcgU2V0KFsiYSIsICJlIiwgImkiLCAibyIsICJ1Il0pOwogIGxldCByZXN1bHQgPSBbXTsKCiAgLy8gdXNpbmcgcmVndWxhciBsb29rdXAgaW5zdGVhZCBvZiAobWFwLHJldmVyc2UpIHRvIHJlZHVjZSB0aGUgY29tcGxleGl0eSAtLSBvKG4pIHRpbWVzCiAgZm9yIChsZXQgaSA9IHMubGVuZ3RoIC0gMTsgaSA+PSAwOyBpLS0pIHsKICAgIGxldCBjaGFyID0gc1tpXTsKICAgIGxldCB1cHBlckNoYXIgPSB2b3dlbHMuaGFzKGNoYXIpID8gY2hhci50b1VwcGVyQ2FzZSgpIDogY2hhcjsKCiAgICBpZiAoIShpID09PSAwICYmICF2b3dlbHMuaGFzKGNoYXIpKSkgcmVzdWx0LnB1c2godXBwZXJDaGFyKTsKICB9CgogIHJldHVybiByZXN1bHQuam9pbigiLSIpOwp9Cgpjb25zb2xlLmxvZyhtYWdpYygiaGVsbG90aGVyZSIpKTsgLy8gIkUtci1FLWgtdC1PLWwtbC1FIgo=