-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-all-adjacent-duplicates-in-string-ii.go
102 lines (83 loc) · 1.67 KB
/
remove-all-adjacent-duplicates-in-string-ii.go
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
package main
import (
"fmt"
"strings"
)
// source: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/
type Stack struct {
values []rune
}
func NewStack() *Stack {
return &Stack{}
}
func (s *Stack) Push(val rune) {
s.values = append(s.values, val)
}
func (s *Stack) Pop() (val rune, ok bool) {
if len(s.values) == 0 {
return val, false
}
val = s.values[len(s.values)-1]
s.values = s.values[:len(s.values)-1]
return val, true
}
func removeDuplicates(s string, k int) string {
var values []rune
push := func(val rune) {
values = append(values, val)
if len(values) < k {
return
}
for i := 1; i < k; i++ {
if values[len(values)-1-i] != val {
return
}
}
values = values[:len(values)-k]
}
for _, r := range s {
push(r)
}
return string(values)
}
// TLE
func removeDuplicates_(s string, k int) string {
var cnt int
var runes []rune
var updated = true
for updated {
runes = []rune(s)
updated = false
cnt = 1
for i := 1; i < len(runes); i++ {
if runes[i] == runes[i-1] {
cnt++
} else {
cnt = 1
}
if cnt == k {
updated = true
cnt = 0
for j := i - k + 1; j <= i; j++ {
runes[j] = '.'
}
}
}
s = strings.ReplaceAll(string(runes), ".", "")
}
return string(runes)
}
func main() {
// Example 2
var s2 string = "deeedbbcccbdaa"
var k2 int = 3
fmt.Println("Expected: \"aa\" Output: ", removeDuplicates(s2, k2))
// Example 3
var s3 string = "pbbcggttciiippooaais"
var k3 int = 2
fmt.Println("Expected: \"ps\" Output: ", removeDuplicates(s3, k3))
// Example 1
var s1 string = "abcd"
var k1 int = 2
fmt.Println("Expected: \"abcd\" Output: ", removeDuplicates(s1, k1))
}