Replace and remove
1class Solution:
2 def replaceAndRemove(self, size, s):
3 if len(s) == 0: return s
4 # a b a c _
5 # a a c _ _
6 # a a _ _ c
7 # a a d d c
8 # d d d d c
9 # remove b first
10 i, a = 0, 0
11 for j in range(size):
12 if s[j] == "a": a += 1
13 if s[j] != "b":
14 s[i] = s[j]
15 i += 1
16
17 curr = i - 1
18 i += a - 1
19 res = i + 1
20 while curr >= 0:
21 if s[curr] == "a":
22 s[i - 1:i + 1] = "dd"
23 i -= 2
24 elif s[curr] != "a":
25 s[i] = s[curr]
26 i -= 1
27 curr -= 1
28
29 return res
30
31