Coding questions · Waymo
Sort a String's Characters by a Given Character Order
Rearrange the characters of a target string so they follow the order given by a second string, placing characters not in that order at the end in their original relative order. Tests custom sort keys, counting, and stable handling of unranked characters.
You are given two strings, target and order. Rearrange the characters of target so that they are sorted according to order: a character that appears earlier in order must come before a character that appears later in order. Return the rearranged string.
Function Signature
def sort_by_order(target: str, order: str) -> str:
Rules
- The result contains exactly the characters of
target, each as many times as it occurs intarget. - Characters of
targetthat appear inordercome first, grouped and arranged by their position inorder. - Characters of
targetthat do not appear inordercome after all of those, in the same relative order in which they appear intarget. ordercontains no repeated characters. It may contain characters that do not occur intarget, and it may be empty.
Constraints
0 <= len(target) <= 10^50 <= len(order) <= 26- Both strings contain only lowercase English letters
atoz.
Examples
Example 1
- Input:
target = "cabbage",order = "bca" - Output:
"bbcaage" - Explanation: The two
bs come first, then thec, then the twoas.gandeare not inorder, so they follow in the order they appear intarget.
Example 2
- Input:
target = "sensor",order = "nrx" - Output:
"nrseso" - Explanation:
nthenrcome first;xdoes not occur intarget. The remaining characterss,e,s,okeep their original relative order.
Example 3
- Input:
target = "abc",order = "" - Output:
"abc"
Custom Order String Rearrangement
Hints
- The alphabet has only 26 letters. What single pass over target tells you everything you need about the letters that appear in order?
- Once you know how many times each listed letter occurs, walking order once lets you emit the whole first part of the answer.
- Letters not in order must keep their original relative order, so collect them as you scan instead of sorting them.
Approach
Mark which letters appear in order using a 26-entry boolean table. Scan target once: for a listed letter, increment its count; for an unlisted letter, append it to a tail buffer, which preserves the unlisted letters' original relative order. Then walk order from left to right and emit each listed letter as many times as it was counted, which groups identical letters and places the groups by their position in order. Finally append the tail. Because identical letters are interchangeable and unlisted letters are kept in source order, this produces the one correct output. This is equivalent to a stable sort keyed by each letter's position in order (with unlisted letters sharing a key larger than every position), but counting avoids the O(n log n) comparison sort.
- Time complexity: O(n + m), where n = len(target) and m = len(order)
- Space complexity: O(n) for the output string (O(1) auxiliary beyond the output and the unlisted-tail buffer, which is itself bounded by n)