Skip to content

Commit 5a4bb1d

Browse files
committedJan 16, 2020
Add solution #791
1 parent 54f38ed commit 5a4bb1d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎solutions/0791-custom-sort-string.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 791. Custom Sort String
3+
* https://leetcode.com/problems/custom-sort-string/
4+
* Difficulty: Medium
5+
*
6+
* S and T are strings composed of lowercase letters. In S,
7+
* no letter occurs more than once.
8+
*
9+
* S was sorted in some custom order previously. We want to
10+
* permute the characters of T so that they match the order
11+
* that S was sorted. More specifically, if x occurs before
12+
* y in S, then x should occur before y in the returned string.
13+
*
14+
* Return any permutation of T (as a string) that satisfies
15+
* this property.
16+
*/
17+
18+
/**
19+
* @param {string} S
20+
* @param {string} T
21+
* @return {string}
22+
*/
23+
var customSortString = function(S, T) {
24+
const order = S.split('').reduce((o, k, i) => ({...o, [k]: i}), {});
25+
return T.split('').sort((a, b) => (order[a] || 0) - (order[b] || 0)).join('');
26+
};

0 commit comments

Comments
 (0)
Please sign in to comment.