Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1155c5

Browse files
committed
Create 791-CustomSortString.py
1 parent 9a21b42 commit c1155c5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎src/791-CustomSortString.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /