-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2213-LongestSubstringOfOneRepeatingCharacter.go
More file actions
126 lines (112 loc) · 4.44 KB
/
2213-LongestSubstringOfOneRepeatingCharacter.go
File metadata and controls
126 lines (112 loc) · 4.44 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
// 2213. Longest Substring of One Repeating Character
// You are given a 0-indexed string s.
// You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indices queryIndices of length k,
// both of which are used to describe k queries.
// The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i].
// Return an array lengths of length k where lengths[i] is the length of the longest substring of s consisting of only one repeating character after the ith query is performed.
// Example 1:
// Input: s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]
// Output: [3,3,4]
// Explanation:
// - 1st query updates s = "bbbacc". The longest substring consisting of one repeating character is "bbb" with length 3.
// - 2nd query updates s = "bbbccc".
// The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3.
// - 3rd query updates s = "bbbbcc". The longest substring consisting of one repeating character is "bbbb" with length 4.
// Thus, we return [3,3,4].
// Example 2:
// Input: s = "abyzz", queryCharacters = "aa", queryIndices = [2,1]
// Output: [2,3]
// Explanation:
// - 1st query updates s = "abazz". The longest substring consisting of one repeating character is "zz" with length 2.
// - 2nd query updates s = "aaazz". The longest substring consisting of one repeating character is "aaa" with length 3.
// Thus, we return [2,3].
// Constraints:
// 1 <= s.length <= 10^5
// s consists of lowercase English letters.
// k == queryCharacters.length == queryIndices.length
// 1 <= k <= 10^5
// queryCharacters consists of lowercase English letters.
// 0 <= queryIndices[i] < s.length
import "fmt"
type SegmentTree struct {
str []byte
mx []int
lmx []int
rmx []int
}
func NewSegmentTree(s string) *SegmentTree {
n := len(s)
t := &SegmentTree{ str: []byte(s), mx: make([]int, n<<2), lmx: make([]int, n<<2), rmx: make([]int, n<<2), }
t.build(0, 0, n - 1)
return t
}
func (t *SegmentTree) build(x, l, r int) {
if l == r {
t.lmx[x] = 1
t.rmx[x] = 1
t.mx[x] = 1
return
}
m := int(uint(l + r) >> 1)
t.build(x * 2 + 1, l, m)
t.build(x * 2 + 2, m + 1, r)
t.pushup(x, l, m, r)
}
func (t *SegmentTree) pushup(x, l, m, r int) {
lch, rch := x*2+1, x*2+2
t.lmx[x] = t.lmx[lch]
t.rmx[x] = t.rmx[rch]
t.mx[x] = max(t.mx[lch], t.mx[rch])
if t.str[m] == t.str[m + 1] { // can be merged
if t.lmx[lch] == m - l + 1 {
t.lmx[x] += t.lmx[rch]
}
if t.rmx[rch] == r - m {
t.rmx[x] += t.rmx[lch]
}
t.mx[x] = max(t.mx[x], t.rmx[lch] + t.lmx[rch])
}
}
func (t *SegmentTree) update(x, l, r, pos int, val byte) {
if l == r {
t.str[pos] = val
return
}
m := int(uint(l+r) >> 1)
if pos <= m {
t.update(x * 2 + 1, l, m, pos, val)
} else {
t.update(x * 2 + 2, m + 1, r, pos, val)
}
t.pushup(x, l, m, r)
}
func longestRepeating(s string, queryCharacters string, queryIndices []int) []int {
res, n := make([]int, len(queryCharacters)), len(s)
t := NewSegmentTree(s)
for i, c := range queryCharacters {
t.update(0, 0, n-1, queryIndices[i], byte(c))
res[i] = t.mx[0]
}
return res
}
func main() {
// Example 1:
// Input: s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]
// Output: [3,3,4]
// Explanation:
// - 1st query updates s = "bbbacc". The longest substring consisting of one repeating character is "bbb" with length 3.
// - 2nd query updates s = "bbbccc".
// The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3.
// - 3rd query updates s = "bbbbcc". The longest substring consisting of one repeating character is "bbbb" with length 4.
// Thus, we return [3,3,4].
fmt.Println(longestRepeating("babacc", "bcb", []int{1,3,3})) // [3,3,4]
// Example 2:
// Input: s = "abyzz", queryCharacters = "aa", queryIndices = [2,1]
// Output: [2,3]
// Explanation:
// - 1st query updates s = "abazz". The longest substring consisting of one repeating character is "zz" with length 2.
// - 2nd query updates s = "aaazz". The longest substring consisting of one repeating character is "aaa" with length 3.
// Thus, we return [2,3].
fmt.Println(longestRepeating("abyzz", "aa", []int{2,1})) // [2,3]
}