-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (85 loc) · 2.58 KB
/
main.go
File metadata and controls
94 lines (85 loc) · 2.58 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Source: https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars
// Title: Lexicographically Minimum String After Removing Stars
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.
//
// While there is a `'*'`, do the following operation:
//
// - Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its left. If there are several smallest characters, you can delete any of them.
//
// Return the **lexicographically smallest** resulting string after removing all `'*'` characters.
//
// A string a is **lexicographically smaller** than a string b if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
// If the first min(a.length, b.length) characters do not differ, then the shorter string is the lexicographically smaller one.
//
// **Example 1:**
//
// ```
// Input: s = "aaba*"
// Output: "aab"
// Explanation:
// We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "abc"
// Output: "abc"
// Explanation:
// There is no `'*'` in the string.
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `s` consists only of lowercase English letters and `'*'`.
// - The input is generated such that it is possible to delete all `'*'` characters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"github.com/emirpasic/gods/trees/binaryheap"
)
// It is always better to remove the right most letter.
func clearStars(s string) string {
n := len(s)
// Heap
type itemType struct {
ch byte
idx int
}
heap := binaryheap.NewWith(func(a, b any) int {
aa, bb := a.(itemType), b.(itemType)
if aa.ch != bb.ch {
return int(aa.ch) - int(bb.ch)
}
return bb.idx - aa.idx
})
// Simulation
removed := make([]bool, n)
for idx := range s {
ch := s[idx]
if ch != '*' {
heap.Push(itemType{ch: ch, idx: idx})
continue
}
removed[idx] = true
// Remove letter
item, ok := heap.Pop()
if !ok {
continue
}
removed[item.(itemType).idx] = true
}
// Answer
ans := make([]byte, 0, n)
for i := range n {
if !removed[i] {
ans = append(ans, s[i])
}
}
return string(ans)
}