Practice/Meta/Leetcode 791. Custom Sort String
CodingMust
You are given two strings: priority and text. Your task is to rearrange the characters in text such that they follow the ordering defined by priority.
Specifically:
priority must maintain their relative order as defined by prioritypriority can be placed anywhere in the resulttext must appear in the output exactly as many times as they appear in the inputFor example, if priority = "bca" and text = "abcd", the output should place all 'b's first, then all 'c's, then all 'a's, and 'd' can go anywhere since it's not in priority.
textpriority must follow the relative order defined by prioritypriority can appear in any positiontextpriority are uniquetextExample 1:
Input: priority = "cba", text = "abcd" Output: "cbad" Explanation: 'c' comes first in priority, then 'b', then 'a'. Character 'd' is not in priority so it can go anywhere.
Example 2:
Input: priority = "xyz", text = "zzyyxx" Output: "xxyyzz" Explanation: All 'x' characters come first (x appears first in priority), then all 'y' characters, then all 'z' characters.
Example 3:
Input: priority = "ace", text = "abcdef" Output: "acebdf" (or "acedbf" or "acebfd", etc.) Explanation: 'a', 'c', 'e' must appear in that order. Characters 'b', 'd', 'f' are not in priority and can be arranged in any order.