-
Notifications
You must be signed in to change notification settings - Fork 376
/
longest_substring_k_replacement_test.go
83 lines (68 loc) · 1.93 KB
/
longest_substring_k_replacement_test.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
/*
Problem:
- Given a string, if you are allowed to replace no more than k letters with
any letter, find the length of the longest substring having the same letters
after replacement.
Example:
- Input: string="aabccbb", k=2
Output: 5
Explanation: Longest substring is "bbbbb" after replacing 2 c with b.
- Input: string="abbcb", k=1
Output: 4
Explanation: Longest substring is "bbbb" after replacing 1 c with b.
Approach:
- Use a hashmap to remember the frequency of each character we have seen.
- As we iterate through the string and add character to the window, we
also keep track of the maximum repeating character count.
- Shrink the window accordingly as we are not allowed to replace more than
k characters.
Cost:
- O(n) time, O(1) space since there are only 26 characters in the alphabet.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestLongestSubstringKReplacement(t *testing.T) {
tests := []struct {
in1 string
in2 int
expected int
}{
{"aabccbb", 2, 5},
{"abbcb", 1, 4},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
longestSubstringKReplacement(tt.in1, tt.in2),
)
}
}
func longestSubstringKReplacement(s string, k int) int {
maxLength, start, maxRepeatCharCount := 0, 0, 0
// char keeps track of characters' frequencies.
char := map[string]int{}
for end := range s {
// insert characters into the frequencies map.
endChar := string(s[end])
if _, ok := char[endChar]; !ok {
char[endChar] = 0
}
char[endChar]++
// update max repeating character count.
maxRepeatCharCount = common.Max(maxRepeatCharCount, char[endChar])
// shrink the window as we are not allowed to replace more than k
// characters.
if end-start+1-maxRepeatCharCount > k {
startChar := string(s[start])
char[startChar]--
start++
}
// update the maximum length at each step.
maxLength = common.Max(maxLength, end-start+1)
}
return maxLength
}