Before attempting this problem, you should be comfortable with:
We want to reorder string s so that characters appear in the same relative order as they do in order. The key insight is that we can assign each character a rank based on its position in order. Characters not in order can be assigned a high rank (like 26) so they appear at the end. By sorting s using these ranks as the comparison key, characters will naturally arrange themselves according to the custom ordering.
order maps to its index (0, 1, 2, ...).order, assign a default rank of 26 (higher than any position in order).s using the rank values as the sorting key.Instead of sorting, we can build the result string directly by counting character frequencies. Since we know the desired order of characters, we first iterate through order and append each character as many times as it appears in s. Then we handle any remaining characters not in order by iterating through the alphabet. This avoids the O(n log n) sorting overhead.
26 to count occurrences of each character in s.order. For each character, append it to the result as many times as its count, then set its count to 0.26 letters. Append any remaining characters (those not in order) to the result based on their counts.class Solution:
def customSortString(self, order: str, s: str) -> str:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
res = []
for c in order:
idx = ord(c) - ord('a')
while count[idx]:
res.append(c)
count[idx] -= 1
for idx in range(26):
c = chr(ord('a') + idx)
while count[idx]:
count[idx] -= 1
res.append(c)
return ''.join(res)Characters in s that do not appear in order must still be included in the result. Forgetting to append these leftover characters produces an incomplete output.
# Wrong: only includes characters from order
for c in order:
res += c * count[c]
# Missing: characters in s but not in order are lost
# Correct: also append remaining characters
for c in order:
res += c * count[ord(c) - ord('a')]
count[ord(c) - ord('a')] = 0
for i in range(26):
res += chr(ord('a') + i) * count[i] # Append leftoversWhen using a comparison-based sort, characters with the same rank (those not in order) may have their relative order changed. While technically valid per the problem, this can cause confusion. Using a counting approach avoids this issue entirely.
Characters not in order need a default rank higher than any position in order. Forgetting to assign a default or using a rank that conflicts with valid positions causes incorrect sorting.
# Wrong: no default rank, causes KeyError
rank = {c: i for i, c in enumerate(order)}
sorted(s, key=lambda c: rank[c]) # Crashes if c not in order
# Correct: provide default rank
sorted(s, key=lambda c: rank.get(c, 26))