Custom Sort String

MediumStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a string order whose characters are all distinct, and a string s. Rearrange the characters of s so that characters appearing in order come out in the same relative order they have in order (all copies of order[0] first, then all copies of order[1], and so on). Characters of s that do not appear in order go **after** all of those, in the same relative order they had in s. Return the rearranged string.

Examples

Input: order = "cba", s = "abcd"
Output: "cbad"
`c`, `b`, `a` follow `order`; `d` is not in `order`, so it goes last.
Input: order = "bcafg", s = "abcd"
Output: "bcad"
`b`, `c`, `a` follow `order`; `f` and `g` do not appear in `s`, and `d` goes last.

Constraints

  • `1 <= order.length <= 26`
  • `1 <= s.length <= 200`
  • `order` and `s` contain only lowercase English letters
  • All characters of `order` are distinct

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems