Count character frequencies in s, then build the result by iterating through order first.
Here's the solution:
function customSortString(order, s):
count = {}
for ch in s:
count[ch] = count.get(ch, 0) + 1
result = []
for ch in order:
if ch in count:
result.append(ch * count[ch])
del count[ch]
for ch in count:
result.append(ch * count[ch])
return "".join(result)
time, space.