-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3039-ApplyOperationsToMakeStringEmpty.go
More file actions
106 lines (92 loc) · 3.2 KB
/
3039-ApplyOperationsToMakeStringEmpty.go
File metadata and controls
106 lines (92 loc) · 3.2 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
95
96
97
98
99
100
101
102
103
104
105
106
package main
// 3039. Apply Operations to Make String Empty
// You are given a string s.
// Consider performing the following operation until s becomes empty:
// For every alphabet character from 'a' to 'z', remove the first occurrence of that character in s (if it exists).
// For example, let initially s = "aabcbbca". We do the following operations:
// 1. Remove the underlined characters s = "aabcbbca". The resulting string is s = "abbca".
// 2. Remove the underlined characters s = "abbca". The resulting string is s = "ba".
// 3. Remove the underlined characters s = "ba". The resulting string is s = "".
// Return the value of the string s right before applying the last operation.
// In the example above, answer is "ba".
// Example 1:
// Input: s = "aabcbbca"
// Output: "ba"
// Explanation: Explained in the statement.
// Input: s = "abcd"
// Output: "abcd"
// Explanation: We do the following operation:
// - Remove the underlined characters s = "abcd". The resulting string is s = "".
// The string just before the last operation is "abcd".
// Constraints:
// 1 <= s.length <= 5 * 10^5
// s consists only of lowercase English letters.
import "fmt"
import "sort"
func lastNonEmptyString(s string) string {
count, pos := make([]int, 26, 26), make([]int, 26, 26)
mx, exist := 0, 0
for i := 0 ; i < 26; i++ {
pos[i] = -1
}
for i := len(s) - 1; i >= 0; i-- {
if pos[s[i]-'a'] == -1 {
pos[s[i] - 'a'] = i
exist++
}
count[s[i] - 'a']++
if count[s[i] - 'a'] > mx {
mx = count[s[i] - 'a']
}
}
str := ""
for i := 0; i < 26; i++ {
if mx == count[i] {
str += string(i + 'a')
}
}
arr := []byte(str)
sort.Slice(arr, func(i, j int) bool {
return pos[arr[i] - 'a'] < pos[arr[j] - 'a']
})
return string(arr)
}
func lastNonEmptyString1(s string) string {
count, pos := make([]int, 26), make([]int, 26)
mx := 0
for i := 0; i < len(s); i++ {
k := int(s[i] - 'a')
count[k]++
pos[k] = i
mx = max(mx, count[k])
}
res := []byte{}
for i := 0; i < 26; i++ {
if count[i] == mx {
res = append(res, 'a' + byte(i))
}
}
sort.Slice(res, func (i, j int) bool{
return pos[int(res[i] - 'a')] < pos[int(res[j] - 'a')]
})
return string(res)
}
func main() {
// Example 1:
// Input: s = "aabcbbca"
// Output: "ba"
// Explanation: Explained in the statement.
fmt.Println(lastNonEmptyString("aabcbbca")) // "ba"
// Input: s = "abcd"
// Output: "abcd"
// Explanation: We do the following operation:
// - Remove the underlined characters s = "abcd". The resulting string is s = "".
// The string just before the last operation is "abcd".
fmt.Println(lastNonEmptyString("abcd")) // "abcd"
fmt.Println(lastNonEmptyString("bluefrog")) // "bluefrog"
fmt.Println(lastNonEmptyString("leetcode")) // "e"
fmt.Println(lastNonEmptyString1("aabcbbca")) // "ba"
fmt.Println(lastNonEmptyString1("abcd")) // "abcd"
fmt.Println(lastNonEmptyString1("bluefrog")) // "bluefrog"
fmt.Println(lastNonEmptyString1("leetcode")) // "e"
}