-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (35 loc) · 896 Bytes
/
Solution.cs
File metadata and controls
36 lines (35 loc) · 896 Bytes
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
public class Solution
{
public string ClearStars(string s)
{
int n = s.Length;
char[] arr = s.ToCharArray();
Stack<int>[] map = new Stack<int>[26];
for (int i = 0; i < 26; i++)
{
map[i] = new();
}
char minCh = 'z';
for (int i = 0; i < n; i++)
{
if (s[i] == '*')
{
while (minCh < 'z' && map[minCh - 'a'].Count <= 0) minCh++;
int idx = map[minCh - 'a'].Pop();
arr[idx] = '*';
}
else
{
map[s[i] - 'a'].Push(i);
if (minCh > s[i]) minCh = s[i];
}
}
StringBuilder sb = new();
for (int i = 0; i < n; i++)
{
if (arr[i] == '*') continue;
sb.Append(arr[i]);
}
return sb.ToString();
}
}