From e92b5b6c0432fb16a2d1aed0ef6f12777f41755c Mon Sep 17 00:00:00 2001 From: YDZ Date: Fri, 14 Jun 2019 14:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=2076?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../76. Minimum Window Substring.go | 40 ++++++++++++++ .../76. Minimum Window Substring_test.go | 53 +++++++++++++++++++ .../0076. Minimum Window Substring/README.md | 29 ++++++++++ 3 files changed, 122 insertions(+) create mode 100644 Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring.go create mode 100644 Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring_test.go create mode 100644 Algorithms/0076. Minimum Window Substring/README.md diff --git a/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring.go b/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring.go new file mode 100644 index 00000000..ece9380c --- /dev/null +++ b/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring.go @@ -0,0 +1,40 @@ +package leetcode + +func minWindow(s string, t string) string { + if s == "" || t == "" { + return "" + } + var tFreq, sFreq [256]int + result, left, right, finalLeft, finalRight, minW, count := "", 0, -1, -1, -1, len(s)+1, 0 + + for i := 0; i < len(t); i++ { + tFreq[t[i]-'a']++ + } + + for left < len(s) { + if right+1 < len(s) && count < len(t) { + sFreq[s[right+1]-'a']++ + if sFreq[s[right+1]-'a'] <= tFreq[s[right+1]-'a'] { + count++ + } + right++ + } else { + if right-left+1 < minW && count == len(t) { + minW = right - left + 1 + finalLeft = left + finalRight = right + } + if sFreq[s[left]-'a'] == tFreq[s[left]-'a'] { + count-- + } + sFreq[s[left]-'a']-- + left++ + } + } + if finalLeft != -1 { + for i := finalLeft; i < finalRight+1; i++ { + result += string(s[i]) + } + } + return result +} diff --git a/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring_test.go b/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring_test.go new file mode 100644 index 00000000..73338f49 --- /dev/null +++ b/Algorithms/0076. Minimum Window Substring/76. Minimum Window Substring_test.go @@ -0,0 +1,53 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question76 struct { + para76 + ans76 +} + +// para 是参数 +// one 代表第一个参数 +type para76 struct { + s string + p string +} + +// ans 是答案 +// one 代表第一个答案 +type ans76 struct { + one string +} + +func Test_Problem76(t *testing.T) { + + qs := []question76{ + + question76{ + para76{"ADOBECODEBANC", "ABC"}, + ans76{"BANC"}, + }, + + question76{ + para76{"a", "aa"}, + ans76{""}, + }, + + question76{ + para76{"a", "a"}, + ans76{"a"}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 76------------------------\n") + + for _, q := range qs { + _, p := q.ans76, q.para76 + fmt.Printf("【input】:%v 【output】:%v\n\n\n", p, minWindow(p.s, p.p)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0076. Minimum Window Substring/README.md b/Algorithms/0076. Minimum Window Substring/README.md new file mode 100644 index 00000000..08f10e78 --- /dev/null +++ b/Algorithms/0076. Minimum Window Substring/README.md @@ -0,0 +1,29 @@ +# [76. Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) + +## 题目 + +Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). + +Example: + +```c +Input: S = "ADOBECODEBANC", T = "ABC" +Output: "BANC" +``` + +Note: + +- If there is no such window in S that covers all characters in T, return the empty string "". +- If there is such window, you are guaranteed that there will always be only one unique minimum window in S. + +## 题目大意 + +给定一个源字符串 s,再给一个字符串 T,要求在源字符串中找到一个窗口,这个窗口包含由字符串各种排列组合组成的,窗口中可以包含 T 中没有的字符,如果存在多个,在结果中输出最小的窗口,如果找不到这样的窗口,输出空字符串。 + +## 解题思路 + +这一题是滑动窗口的题目,在窗口滑动的过程中不断的包含字符串 T,直到完全包含字符串 T 的字符以后,记下左右窗口的位置和窗口大小。每次都不断更新这个符合条件的窗口和窗口大小的最小值。最后输出结果即可。 + + + +