forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#981 from andra961/main
Create: 76-Minimum-Window-Substring.ts
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function minWindow(s: string, t: string): string { | ||
let minL = 0; | ||
let minR = s.length; | ||
|
||
//create string t hashmap and needed | ||
const tCount = {}; | ||
let needed = 0; | ||
for (let i = 0; i < t.length; i++) { | ||
if (t[i] in tCount) tCount[t[i]]++; | ||
else { | ||
tCount[t[i]] = 1; | ||
needed++; | ||
} | ||
} | ||
|
||
//initialize winCount(empty) | ||
const winCount = {}; | ||
let matched = 0; | ||
|
||
let l = 0; | ||
|
||
for (let r = 0; r < s.length; r++) { | ||
//update winCount with adding s[r] | ||
if (s[r] in winCount) winCount[s[r]]++; | ||
else winCount[s[r]] = 1; | ||
//update matched | ||
if (s[r] in tCount && winCount[s[r]] === tCount[s[r]]) matched++; | ||
|
||
//the window is valid | ||
while (matched === needed) { | ||
//update min | ||
if (r - l + 1 < minR - minL + 1) { | ||
minL = l; | ||
minR = r; | ||
} | ||
|
||
//remove l | ||
//update matched | ||
if (s[l] in tCount && winCount[s[l]] === tCount[s[l]]) matched--; | ||
//update winCount with the removal of s[l] | ||
winCount[s[l]]--; | ||
if (winCount[s[l]] === 0) delete winCount[s[l]]; | ||
l++; | ||
} | ||
} | ||
|
||
return minR - minL + 1 > s.length ? '' : s.slice(minL, minR + 1); | ||
} |