791. Custom Sort String

202406272005
tags: #bucket-sort

var customSortString = function(order, s) {
	const freqCounts = new Map();
	for (const c of order) {
		freqCounts.set(c, 0);
	}
	for (const c of s) {
		freqCounts.set(c, (freqCounts.get(c) ?? 0) + 1);
	}
	let res = "";
	for (const c of order) {
		res += c.repeat(freqCounts.get(c));
	}
	for (const c of s) {
		if (!order.includes(c)) {
			res += c;
		}
	}
	return res;
};

Reference

https://leetcode.com/problems/custom-sort-string/