-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path424.Longest-Repeating-Character-Replacement.go
121 lines (103 loc) · 2.25 KB
/
424.Longest-Repeating-Character-Replacement.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// https://leetcode.com/problems/longest-repeating-character-replacement/
//
// algorithms
// Medium (43.16%)
// Total Accepted: 24,824
// Total Submissions: 57,513
// 我的做法就不聊了,看完 100% 的做法觉得贼愚蠢
package leetcode
type repeatSubStr struct {
beginIdx int
endIdx int
}
func characterReplacement(s string, k int) int {
length := len(s)
if length < 2 || k >= length {
return length
}
hashMap := make(map[byte][]repeatSubStr)
cntCh, cntIdx := s[0], 0
for i := 1; i < length; i++ {
if s[i] == cntCh {
continue
} else {
if _, ok := hashMap[cntCh]; ok {
hashMap[cntCh] = append(hashMap[cntCh], repeatSubStr{cntIdx, i - 1})
} else {
hashMap[cntCh] = []repeatSubStr{{cntIdx, i - 1}}
}
cntCh = s[i]
cntIdx = i
}
}
if _, ok := hashMap[cntCh]; ok {
hashMap[cntCh] = append(hashMap[cntCh], repeatSubStr{cntIdx, length - 1})
} else {
hashMap[cntCh] = []repeatSubStr{{cntIdx, length - 1}}
}
res := 0
for _, arr := range hashMap {
arrLen := len(arr)
tmp := arr[0].endIdx - arr[0].beginIdx + 1
if arrLen == 1 {
res = max(res, tmp+k)
}
cntK := k
head, tail := 0, 1
for tail < arrLen {
tailGap := arr[tail].beginIdx - arr[tail-1].endIdx - 1
if cntK >= tailGap {
tmp += tailGap + arr[tail].endIdx - arr[tail].beginIdx + 1
cntK -= tailGap
tail++
} else if tail-head == 1 {
res = max(res, tmp+cntK)
tmp = arr[tail].endIdx - arr[tail].beginIdx + 1
head++
tail++
} else {
headGap := arr[head+1].beginIdx - arr[head].endIdx - 1
cntK += headGap
tmp -= arr[head+1].beginIdx - arr[head].beginIdx
head++
}
res = max(res, tmp+cntK)
}
}
if res > length {
res = length
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// 这是 100% 的做法
func characterReplacement(s string, k int) int {
n := len(s)
if n == 0 {
return 0
}
begin := 0
charCounts := make([]int, 26)
maxCharCount := 0
var ret int
for end := 0; end < n; end++ {
c := s[end]
charCounts[c-'A']++
if charCounts[c-'A'] > maxCharCount {
maxCharCount = charCounts[c-'A']
}
for end-begin+1-maxCharCount > k {
charCounts[s[begin]-'A']--
begin++
}
if end-begin+1 > ret {
ret = end - begin + 1
}
}
return ret
}