Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5607eb3

Browse files
Create minimum-window-substring.go
1 parent 8580ca6 commit 5607eb3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎minimum-window-substring.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//https://leetcode.com/problems/minimum-window-substring/
2+
3+
package leetcode_solutions_golang
4+
5+
func minWindow(s string, t string) string {
6+
targetCount := [58]int{}
7+
curCount := [58]int{}
8+
for _, i := range t {
9+
targetCount[i-'A']++
10+
}
11+
left, right := 0, 0
12+
answer := ""
13+
for left < len(s) {
14+
for right < len(s) && !isTargetMet(curCount, targetCount) {
15+
if targetCount[s[right]-'A'] > 0 {
16+
curCount[s[right]-'A']++
17+
}
18+
right++
19+
}
20+
if isTargetMet(curCount, targetCount) && (answer == "" || len(answer) > right-left) {
21+
answer = s[left:right]
22+
}
23+
if targetCount[s[left]-'A'] > 0 {
24+
curCount[s[left]-'A']--
25+
}
26+
left++
27+
}
28+
return answer
29+
}
30+
31+
func isTargetMet(curCount, targetCount [58]int) bool {
32+
for i := 0; i < 58; i++ {
33+
if curCount[i] < targetCount[i] {
34+
return false
35+
}
36+
}
37+
return true
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /