leetcode/0791.Custom-Sort-String/README.md
order and str are strings composed of lowercase letters. In order, no letter occurs more than once.
order was sorted in some custom order previously. We want to permute the characters of str so that they match the order that order was sorted. More specifically, if x occurs before y in order, then x should occur before y in the returned string.
Return any permutation of str (as a string) that satisfies this property.
Example:Input:
order = "cba"
str = "abcd"
Output: "cbad"
Explanation:
"a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
Note:
order has length at most 26, and no character is repeated in order.str has length at most 200.order and str consist of lowercase letters only.字符串 S 和 T 只包含小写字符。在 S 中,所有字符只会出现一次。S 已经根据某种规则进行了排序。我们要根据 S 中的字符顺序对 T 进行排序。更具体地说,如果 S 中 x 在 y 之前出现,那么返回的字符串中 x 也应出现在 y 之前。返回任意一种符合条件的字符串 T。
package leetcode
import "sort"
func customSortString(order string, str string) string {
magic := map[byte]int{}
for i := range order {
magic[order[i]] = i - 30
}
byteSlice := []byte(str)
sort.Slice(byteSlice, func(i, j int) bool {
return magic[byteSlice[i]] < magic[byteSlice[j]]
})
return string(byteSlice)
}