custom_sort_string
1from functools import cmp_to_key 2 3 4# @leet start 5class Solution: 6 def customSortString(self, order: str, s: str) -> str: 7 """ 8 This question asks us to sort a string given a particular sorted order, 9 passed in the function as a parameter. If a char is not in the sorted 10 order provided, it can be placed anywhere in the string. 11 12 First we create a dictionary of char -> index to use later. 13 Then we define a custom sorting function that returns -1, 0, or 1, and 14 then use `cmp_to_key` from functools to turn that into a sorting function. 15 16 We then sort the sortable characters and add the unsortable characters 17 to the end of the string. 18 """ 19 sort_order = {c: i for i, c in enumerate(order)} 20 21 def sorter(a, b): 22 if a == b: 23 return 0 24 if sort_order[a] > sort_order[b]: 25 return 1 26 return -1 27 28 sortable = [] 29 unsortable = [] 30 for c in s: 31 if c in sort_order: 32 sortable.append(c) 33 else: 34 unsortable.append(c) 35 36 sortable.sort(key=cmp_to_key(sorter)) 37 return "".join(sortable + unsortable) 38 39 40# @leet end 41 42 43def test(): 44 assert 2 + 2 == 4
class
Solution:
6class Solution: 7 def customSortString(self, order: str, s: str) -> str: 8 """ 9 This question asks us to sort a string given a particular sorted order, 10 passed in the function as a parameter. If a char is not in the sorted 11 order provided, it can be placed anywhere in the string. 12 13 First we create a dictionary of char -> index to use later. 14 Then we define a custom sorting function that returns -1, 0, or 1, and 15 then use `cmp_to_key` from functools to turn that into a sorting function. 16 17 We then sort the sortable characters and add the unsortable characters 18 to the end of the string. 19 """ 20 sort_order = {c: i for i, c in enumerate(order)} 21 22 def sorter(a, b): 23 if a == b: 24 return 0 25 if sort_order[a] > sort_order[b]: 26 return 1 27 return -1 28 29 sortable = [] 30 unsortable = [] 31 for c in s: 32 if c in sort_order: 33 sortable.append(c) 34 else: 35 unsortable.append(c) 36 37 sortable.sort(key=cmp_to_key(sorter)) 38 return "".join(sortable + unsortable)
def
customSortString(self, order: str, s: str) -> str:
7 def customSortString(self, order: str, s: str) -> str: 8 """ 9 This question asks us to sort a string given a particular sorted order, 10 passed in the function as a parameter. If a char is not in the sorted 11 order provided, it can be placed anywhere in the string. 12 13 First we create a dictionary of char -> index to use later. 14 Then we define a custom sorting function that returns -1, 0, or 1, and 15 then use `cmp_to_key` from functools to turn that into a sorting function. 16 17 We then sort the sortable characters and add the unsortable characters 18 to the end of the string. 19 """ 20 sort_order = {c: i for i, c in enumerate(order)} 21 22 def sorter(a, b): 23 if a == b: 24 return 0 25 if sort_order[a] > sort_order[b]: 26 return 1 27 return -1 28 29 sortable = [] 30 unsortable = [] 31 for c in s: 32 if c in sort_order: 33 sortable.append(c) 34 else: 35 unsortable.append(c) 36 37 sortable.sort(key=cmp_to_key(sorter)) 38 return "".join(sortable + unsortable)
This question asks us to sort a string given a particular sorted order, passed in the function as a parameter. If a char is not in the sorted order provided, it can be placed anywhere in the string.
First we create a dictionary of char -> index to use later.
Then we define a custom sorting function that returns -1, 0, or 1, and
then use cmp_to_key from functools to turn that into a sorting function.
We then sort the sortable characters and add the unsortable characters to the end of the string.
def
test():