From ab925096f771a76b7a90479d760c57c7d50b769f Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Wed, 16 Jun 2021 17:00:13 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0076.Minimum Window Substring --- .../0076.Minimum Window Substring/README.md | 42 +++++++++++++++++++ .../README_EN.md | 40 ++++++++++++++++++ .../0076.Minimum Window Substring/Solution.ts | 35 ++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 solution/0000-0099/0076.Minimum Window Substring/Solution.ts diff --git a/solution/0000-0099/0076.Minimum Window Substring/README.md b/solution/0000-0099/0076.Minimum Window Substring/README.md index 4b537ec094db0..c9366bd46f012 100644 --- a/solution/0000-0099/0076.Minimum Window Substring/README.md +++ b/solution/0000-0099/0076.Minimum Window Substring/README.md @@ -40,6 +40,8 @@ ## 解法 +滑动窗口 + @@ -60,6 +62,46 @@ ``` +### **TypeScript** + +```ts +function minWindow(s: string, t: string): string { + let n1 = s.length, n2 = t.length; + if (n1 < n2) return ''; + let need = new Array(128).fill(0); + let window = new Array(128).fill(0); + for (let i = 0; i < n2; ++i) { + ++need[t.charCodeAt(i)]; + } + + let left = 0, right = 0; + let res = ''; + let count = 0; + let min = n1 + 1; + while (right < n1) { + let cur = s.charCodeAt(right); + ++window[cur]; + if (need[cur] > 0 && need[cur] >= window[cur]) { + ++count; + } + while (count == n2) { + cur = s.charCodeAt(left); + if (need[cur] > 0 && need[cur] >= window[cur]) { + --count; + } + if (right - left + 1 < min) { + min = right - left + 1; + res = s.slice(left, right + 1); + } + --window[cur]; + ++left; + } + ++right; + } + return res; +}; +``` + ### **...** ``` diff --git a/solution/0000-0099/0076.Minimum Window Substring/README_EN.md b/solution/0000-0099/0076.Minimum Window Substring/README_EN.md index 437d581fb107e..c7ceb32f969eb 100644 --- a/solution/0000-0099/0076.Minimum Window Substring/README_EN.md +++ b/solution/0000-0099/0076.Minimum Window Substring/README_EN.md @@ -43,6 +43,46 @@ ``` +### **TypeScript** + +```ts +function minWindow(s: string, t: string): string { + let n1 = s.length, n2 = t.length; + if (n1 < n2) return ''; + let need = new Array(128).fill(0); + let window = new Array(128).fill(0); + for (let i = 0; i < n2; ++i) { + ++need[t.charCodeAt(i)]; + } + + let left = 0, right = 0; + let res = ''; + let count = 0; + let min = n1 + 1; + while (right < n1) { + let cur = s.charCodeAt(right); + ++window[cur]; + if (need[cur] > 0 && need[cur] >= window[cur]) { + ++count; + } + while (count == n2) { + cur = s.charCodeAt(left); + if (need[cur] > 0 && need[cur] >= window[cur]) { + --count; + } + if (right - left + 1 < min) { + min = right - left + 1; + res = s.slice(left, right + 1); + } + --window[cur]; + ++left; + } + ++right; + } + return res; +}; +``` + ### **...** ``` diff --git a/solution/0000-0099/0076.Minimum Window Substring/Solution.ts b/solution/0000-0099/0076.Minimum Window Substring/Solution.ts new file mode 100644 index 0000000000000..fe907c87f84a9 --- /dev/null +++ b/solution/0000-0099/0076.Minimum Window Substring/Solution.ts @@ -0,0 +1,35 @@ +function minWindow(s: string, t: string): string { + let n1 = s.length, n2 = t.length; + if (n1 < n2) return ''; + let need = new Array(128).fill(0); + let window = new Array(128).fill(0); + for (let i = 0; i < n2; ++i) { + ++need[t.charCodeAt(i)]; + } + + let left = 0, right = 0; + let res = ''; + let count = 0; + let min = n1 + 1; + while (right < n1) { + let cur = s.charCodeAt(right); + ++window[cur]; + if (need[cur] > 0 && need[cur] >= window[cur]) { + ++count; + } + while (count == n2) { + cur = s.charCodeAt(left); + if (need[cur] > 0 && need[cur] >= window[cur]) { + --count; + } + if (right - left + 1 < min) { + min = right - left + 1; + res = s.slice(left, right + 1); + } + --window[cur]; + ++left; + } + ++right; + } + return res; +}; \ No newline at end of file