Skip to content

Commit d3a8277

Browse files
committed
Runtime: 72 ms (Top 48.8%) | Memory: 17.70 MB (Top 9.21%)
1 parent 6b5cee1 commit d3a8277

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
from collections import Counter
1+
// Runtime: 72 ms (Top 48.8%) | Memory: 17.70 MB (Top 9.21%)
2+
23
class Solution:
34
def sortString(self, s: str) -> str:
4-
counter = Counter(s)
5-
alphabets = "abcdefghijklmnopqrstuvwxyz"
6-
rev_alphabets = alphabets[::-1]
7-
total = len(s)
8-
res = []
9-
while total > 0:
10-
for c in alphabets:
11-
if counter[c]:
12-
res.append(c)
13-
counter[c] -= 1
14-
total -= 1
15-
for c in rev_alphabets:
16-
if counter[c]:
17-
res.append(c)
18-
counter[c] -= 1
19-
total -= 1
20-
return "".join(res)
5+
s = list(s)
6+
result = ''
7+
while s:
8+
for letter in sorted(set(s)):
9+
s.remove(letter)
10+
result += letter
11+
for letter in sorted(set(s), reverse=True):
12+
s.remove(letter)
13+
result += letter
14+
return result

0 commit comments

Comments
 (0)