791. Custom Sort String
SandTare strings composed of lowercase letters. InS, no letter occurs more than once.
Swas sorted in some custom order previously. We want to permute the characters ofTso that they match the order thatSwas sorted. More specifically, ifxoccurs beforeyinS, thenxshould occur beforeyin the returned string.
Return any permutation ofT(as a string) that satisfies this property.
Example :
Input:
S = "cba"
T = "abcd"
Output:
"cbad"
Explanation:
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.Note:
Shas length at most26, and no character is repeated inS.Thas length at most200.SandTconsist of lowercase letters only.
Code : Python counting sort
Code: C++ with Lambda funciton:
Code: C++ with vector<int> as counting bucket
Code: C++ building map with transform & sort the T
Code: Java Counting Sort using int[]
Last updated
Was this helpful?