Created
May 3, 2021 14:00
-
-
Save leonidkuznetsov18/427afbc30d0c53fcf75538eba0d70988 to your computer and use it in GitHub Desktop.
Minimum Window Substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string} s | |
| * @param {string} t | |
| * @return {string} | |
| */ | |
| var minWindow = function(s, t) { | |
| let map = new Map(); | |
| let sLen = s.length; | |
| let tLen = t.length; | |
| let count = tLen; | |
| let min = Number.MAX_SAFE_INTEGER; | |
| let head = 0; | |
| let left = 0; | |
| let right = 0; | |
| if (!sLen || !tLen) { | |
| return ''; | |
| } | |
| for (let i = 0; i < tLen; i++) { | |
| if (map.get(t[i]) === undefined) { | |
| map.set(t[i], 1) | |
| } else { | |
| map.set(t[i], map.get(t[i]) + 1); | |
| } | |
| } | |
| while (right < sLen) { | |
| if (map.get(s[right]) !== undefined) { | |
| if (map.get(s[right]) > 0) { | |
| count--; | |
| } | |
| map.set(s[right], map.get(s[right]) - 1); | |
| } | |
| right++; | |
| while (count === 0) { | |
| if (right - left < min) { | |
| min = right - left; | |
| head = left; | |
| } | |
| if (map.get(s[left]) !== undefined) { | |
| if (map.get(s[left]) === 0) { | |
| count++; | |
| } | |
| map.set(s[left], map.get(s[left]) + 1) | |
| } | |
| left++; | |
| } | |
| } | |
| return min === Number.MAX_SAFE_INTEGER ? '' : s.substr(head, min); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment