Skip to content

Commit 9acfff3

Browse files
authored
Merge pull request neetcode-gh#1124 from imrushi/main
Go: 424-Longest Repeating Character Replacement & 567-Permutation in String
2 parents 32537ad + 278092a commit 9acfff3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func characterReplacement(s string, k int) int {
2+
count := make(map[byte]int)
3+
res := 0
4+
5+
l := 0
6+
maxf := 0
7+
for r, _ := range s {
8+
count[s[r]] = 1 + count[s[r]]
9+
maxf = max(maxf, count[s[r]])
10+
11+
if (r - l + 1) - maxf > k{
12+
count[s[l]] -= 1
13+
l++
14+
}
15+
res = max(res, r - l + 1)
16+
}
17+
return res
18+
}
19+
20+
func max(a,b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}

go/567-Permutation-In-String.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func checkInclusion(s1 string, s2 string) bool {
2+
if len(s1) > len(s2){
3+
return false
4+
}
5+
6+
s1Count, s2Count := [26]int{}, [26]int{}
7+
for i, _ := range s1 {
8+
s1Count[s1[i] - 'a']++
9+
s2Count[s2[i] - 'a']++
10+
}
11+
matches := 0
12+
for i:=0;i<26;i++ {
13+
if s1Count[i] == s2Count[i] {
14+
matches += 1
15+
} else {
16+
matches += 0
17+
}
18+
}
19+
20+
l := 0
21+
for r:=len(s1);r<len(s2);r++{
22+
if matches == 26 {
23+
return true
24+
}
25+
26+
index := s2[r] - 'a'
27+
s2Count[index]++
28+
if s1Count[index] == s2Count[index]{
29+
matches++
30+
} else if s1Count[index] + 1 == s2Count[index]{
31+
matches--
32+
}
33+
34+
index = s2[l] - 'a'
35+
s2Count[index]--
36+
if s1Count[index] == s2Count[index]{
37+
matches++
38+
} else if s1Count[index] - 1 == s2Count[index]{
39+
matches--
40+
}
41+
l++
42+
}
43+
return matches == 26
44+
}

0 commit comments

Comments
 (0)