Skip to content

Commit

Permalink
feat: solve problem 78
Browse files Browse the repository at this point in the history
  • Loading branch information
orgball2608 committed Sep 7, 2024
1 parent 3c6f4a2 commit 6b3aa37
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions hard/76. Minimum Window Substring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hard

import "math"

func minWindow(s string, t string) string {
if len(s) == 0 || len(t) == 0 || len(s) < len(t) {
return ""
}

mapStr := make([]int, 128)
count := len(t)
start, end := 0, 0
minLen, startIndex := math.MaxInt32, 0
for _, val := range t {
mapStr[val]++
}

for end < len(s) {
if mapStr[s[end]] > 0 {
count--
}
mapStr[s[end]]--
end++

for count == 0 {
if end-start < minLen {
minLen = end - start + 1
startIndex = start
}

if mapStr[s[start]] == 0 {
count++
}
mapStr[s[start]]++
start++
}
}

if minLen == math.MaxInt32 {
return ""
}

return s[startIndex : startIndex+minLen-1]
}

//Time: 0(N)
//Space: O(1)

0 comments on commit 6b3aa37

Please sign in to comment.