All problems

Problem 526

Custom Sort String

Medium · 20 pts · String, Sorting, Hash Table

Problem

You are given a string ORDER of distinct lowercase letters and a string S. Rearrange S so that the letters that appear in ORDER come first, in the same relative order as in ORDER (all copies of a letter together). Letters of S that do not appear in ORDER go after them, keeping the order in which they appear in S. Count each letter of S, write the ORDER letters by count, then append the remaining letters as they appear.

Input

The first line contains ORDER. The second line contains S.

Output

Print the rearranged string.

Constraints

1 <= |ORDER| <= 26 (distinct letters); 1 <= |S| <= 100000

Example input

cba
abcd

Example output

cbad

Explanation

With ORDER = cba and S = abcd, the letters c, b, a come first in that order, giving cba, and d is not in ORDER so it goes at the end. The answer is cbad.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Student Attendance Record Next: Minimum Time to Type Word Using Special Typewriter