|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +CREATED AT: 2022年11月13日 |
| 4 | + |
| 5 | +URL: https://leetcode.com/problems/custom-sort-string/ |
| 6 | + |
| 7 | +GITHUB: https://github.com/Jiezhi/myleetcode |
| 8 | + |
| 9 | +FileName: 791-CustomSortString |
| 10 | + |
| 11 | +Difficulty: Medium |
| 12 | + |
| 13 | +Desc: |
| 14 | + |
| 15 | +Tag: |
| 16 | + |
| 17 | +See: |
| 18 | + |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def customSortString(self, order: str, s: str) -> str: |
| 24 | + """ |
| 25 | + Runtime: 59 ms, faster than 45.64% |
| 26 | + Memory Usage: 13.8 MB, less than 98.17% |
| 27 | + |
| 28 | + 1 <= order.length <= 26 |
| 29 | + 1 <= s.length <= 200 |
| 30 | + order and s consist of lowercase English letters. |
| 31 | + All the characters of order are unique. |
| 32 | + """ |
| 33 | + o = {c: i for i, c in enumerate(order)} |
| 34 | + s = sorted([o[c] if c in o else -1, c] for c in s) |
| 35 | + return ''.join(c[1] for c in s) |
| 36 | + |
| 37 | + |
| 38 | +def test(): |
| 39 | + assert Solution().customSortString(order="cbafg", s="abcd") == "dcba" |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == '__main__': |
| 43 | + test() |
0 commit comments