Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1175 from syedalisait/main
Browse files Browse the repository at this point in the history
Go: 76. Minimum Window Substring
  • Loading branch information
Ahmad-A0 authored Sep 24, 2022
2 parents 27bed34 + 4ce0c86 commit 98c1db7
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions go/76-Minimum-Window-Substring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
func minWindow(s string, t string) string {
start, end := 0, 0
targetCharacterFrequency := make(map[uint8]int)
currentCharacterFrequency := make(map[uint8]int)
distinctCharacterCount := 0
minSubstring := ""

for index := range t {
targetCharacterFrequency[t[index]]++
}

for end < len(s) {
currentCharacterFrequency[s[end]]++
if targetCharacterFrequency[s[end]] != 0 &&
targetCharacterFrequency[s[end]] == currentCharacterFrequency[s[end]] {
distinctCharacterCount++
}

for distinctCharacterCount == len(targetCharacterFrequency) {
if minSubstring == "" {
minSubstring = s[start:end+1]
}
if end - start + 1 < len(minSubstring) {
minSubstring = s[start:end+1]
}

currentCharacterFrequency[s[start]]--
if currentCharacterFrequency[s[start]] < targetCharacterFrequency[s[start]] {
distinctCharacterCount--
}

start++
}
end++
}

return minSubstring
}

0 comments on commit 98c1db7

Please sign in to comment.