-
Notifications
You must be signed in to change notification settings - Fork 17
/
increasing-decreasing-string.py
51 lines (34 loc) · 1.16 KB
/
increasing-decreasing-string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from collections import Counter
class Solution:
def sortString(self, s: str) -> str:
chars = []
count = Counter()
for c in s:
if count[c] == 0:
chars.append(c)
count[c] += 1
chars.sort()
result_arr = []
direction = True
while any([count[c] > 0 for c in chars]):
for c in chars if direction else reversed(chars):
if count[c] > 0:
result_arr.append(c)
count[c] -= 1
direction = not direction
return "".join(result_arr)
class TestSolution:
def setup(self):
self.sol = Solution()
def test_empty(self):
assert self.sol.sortString("") == ""
def test_case1(self):
assert self.sol.sortString("aaaabbbbcccc") == "abccbaabccba"
def test_case2(self):
assert self.sol.sortString("rat") == "art"
def test_case3(self):
assert self.sol.sortString("leetcode") == "cdelotee"
def test_case4(self):
assert self.sol.sortString("ggggggg") == "ggggggg"
def test_case4(self):
assert self.sol.sortString("spo") == "ops"