File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
Medium/3170. Lexicographically Minimum String After Removing Stars Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ import heapq
2
+ from collections import defaultdict , deque
3
+
4
+ class Solution (object ):
5
+ def clearStars (self , s ):
6
+ n = len (s )
7
+ pq = [] # Min-heap to store characters
8
+ m = defaultdict (deque ) # Map character -> deque of indices
9
+ keep = [True ] * n # Marks whether to keep character at index
10
+
11
+ for i in range (n ):
12
+ if s [i ] == '*' :
13
+ smallest = heapq .heappop (pq ) # Get smallest character
14
+ idx = m [smallest ].pop () # Remove last occurrence index
15
+ keep [i ] = False # Mark '*' for removal
16
+ keep [idx ] = False # Mark smallest char for removal
17
+ else :
18
+ heapq .heappush (pq , s [i ])
19
+ m [s [i ]].append (i )
20
+
21
+ # Build result from characters not marked for removal
22
+ return '' .join (s [i ] for i in range (n ) if keep [i ])
You can’t perform that action at this time.
0 commit comments