Let's trace order = "bca", s = "aaabbc".
Count characters in s: {a: 3, b: 2, c: 1}.
Iterate through order:
'b': append "bb", result = "bb". Mark b as done.
'c': append "c", result = "bbc". Mark c as done.
'a': append "aaa", result = "bbcaaa". Mark a as done.
No remaining characters. Final answer: "bbcaaa".
Counting takes . Iterating through order is . Building the result is . Total: time, space for the output.