The Problem

Sort the letters in the string s by the order they occur in the string t.

Example

  • For s = "weather" and t = "therapyw", the output should be
    sortByString(s, t) = "theeraw";
  • For s = "good" and t = "odg", the output should be
    sortByString(s, t) = "oodg".

Input/Output

  • [time limit] 4000ms (py)
  • [input] string s

    A string consisting only of lowercase English letters.

    Constraints:
    0 ≤ s.length ≤ 104.

  • [input] string t

    A string consisting only of unique lowercase English letters. It is guaranteed that t contains all of the letters that occur in s.

    Constraints:
    0 ≤ t.length ≤ 26.

  • [output] string

The Solution

Capture.PNG

The Explanation

We have a string input, using a custom lambda function that uses the indexes of letters in the second string (using find function,) we sort the letters. sorted function returns an array and we need to convert it to a string. We do this by calling join without putting any delimiters.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s