Minimum Window Substring in JavaScript
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I find this Substring Leetcode challenge online. 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^2)$ or $O(n)$ (optimized).
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
I did the following approach. Moving Window problems. I also test my code on repl.it.
function minimumWindowSubstring(S, T)
let result = [0, Infinity]
let counts = ;
let missingCharacters = T.length;
// Create the counts hash table
for(let i = 0; i < T.length; i++)
counts[T[i]] = 0;
let slow = 0;
for(let fast = 0; fast < S.length; fast++)
// Check if character at fast index is incounts hash
if(S[fast] in counts)
// If you haven't seen that character before
if(counts[S[fast]] === 0)
// Decrement number of missing characters
missingCharacters -= 1;
// And add one to its counts value
counts[S[fast]] += 1
// Shrink window until you have an incomplete set
while(missingCharacters === 0)
// Updates result range if smaller than previous range
if((fast - slow) < (result[1] - result[0]))
result[0] = slow
result[1] = fast
if(S[slow] in counts)
counts[S[slow]] -= 1
if(counts[S[slow]] === 0)
missingCharacters += 1
slow += 1;
return result[1] === Infinity ? "" : S.slice(result[0], result[1] + 1);
console.log(minimumWindowSubstring("ADOBECODEBANC", "ABC")) // "BANC"
javascript strings
add a comment |Â
up vote
0
down vote
favorite
I find this Substring Leetcode challenge online. 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^2)$ or $O(n)$ (optimized).
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
I did the following approach. Moving Window problems. I also test my code on repl.it.
function minimumWindowSubstring(S, T)
let result = [0, Infinity]
let counts = ;
let missingCharacters = T.length;
// Create the counts hash table
for(let i = 0; i < T.length; i++)
counts[T[i]] = 0;
let slow = 0;
for(let fast = 0; fast < S.length; fast++)
// Check if character at fast index is incounts hash
if(S[fast] in counts)
// If you haven't seen that character before
if(counts[S[fast]] === 0)
// Decrement number of missing characters
missingCharacters -= 1;
// And add one to its counts value
counts[S[fast]] += 1
// Shrink window until you have an incomplete set
while(missingCharacters === 0)
// Updates result range if smaller than previous range
if((fast - slow) < (result[1] - result[0]))
result[0] = slow
result[1] = fast
if(S[slow] in counts)
counts[S[slow]] -= 1
if(counts[S[slow]] === 0)
missingCharacters += 1
slow += 1;
return result[1] === Infinity ? "" : S.slice(result[0], result[1] + 1);
console.log(minimumWindowSubstring("ADOBECODEBANC", "ABC")) // "BANC"
javascript strings
2
What isn
in the complexity O(n)? Length ofS
? length ofT
? size of an alphabet?
â vnp
Jul 30 at 20:49
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I find this Substring Leetcode challenge online. 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^2)$ or $O(n)$ (optimized).
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
I did the following approach. Moving Window problems. I also test my code on repl.it.
function minimumWindowSubstring(S, T)
let result = [0, Infinity]
let counts = ;
let missingCharacters = T.length;
// Create the counts hash table
for(let i = 0; i < T.length; i++)
counts[T[i]] = 0;
let slow = 0;
for(let fast = 0; fast < S.length; fast++)
// Check if character at fast index is incounts hash
if(S[fast] in counts)
// If you haven't seen that character before
if(counts[S[fast]] === 0)
// Decrement number of missing characters
missingCharacters -= 1;
// And add one to its counts value
counts[S[fast]] += 1
// Shrink window until you have an incomplete set
while(missingCharacters === 0)
// Updates result range if smaller than previous range
if((fast - slow) < (result[1] - result[0]))
result[0] = slow
result[1] = fast
if(S[slow] in counts)
counts[S[slow]] -= 1
if(counts[S[slow]] === 0)
missingCharacters += 1
slow += 1;
return result[1] === Infinity ? "" : S.slice(result[0], result[1] + 1);
console.log(minimumWindowSubstring("ADOBECODEBANC", "ABC")) // "BANC"
javascript strings
I find this Substring Leetcode challenge online. 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^2)$ or $O(n)$ (optimized).
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
I did the following approach. Moving Window problems. I also test my code on repl.it.
function minimumWindowSubstring(S, T)
let result = [0, Infinity]
let counts = ;
let missingCharacters = T.length;
// Create the counts hash table
for(let i = 0; i < T.length; i++)
counts[T[i]] = 0;
let slow = 0;
for(let fast = 0; fast < S.length; fast++)
// Check if character at fast index is incounts hash
if(S[fast] in counts)
// If you haven't seen that character before
if(counts[S[fast]] === 0)
// Decrement number of missing characters
missingCharacters -= 1;
// And add one to its counts value
counts[S[fast]] += 1
// Shrink window until you have an incomplete set
while(missingCharacters === 0)
// Updates result range if smaller than previous range
if((fast - slow) < (result[1] - result[0]))
result[0] = slow
result[1] = fast
if(S[slow] in counts)
counts[S[slow]] -= 1
if(counts[S[slow]] === 0)
missingCharacters += 1
slow += 1;
return result[1] === Infinity ? "" : S.slice(result[0], result[1] + 1);
console.log(minimumWindowSubstring("ADOBECODEBANC", "ABC")) // "BANC"
javascript strings
edited Jul 31 at 4:14
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jul 30 at 20:19
NinjaG
634221
634221
2
What isn
in the complexity O(n)? Length ofS
? length ofT
? size of an alphabet?
â vnp
Jul 30 at 20:49
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50
add a comment |Â
2
What isn
in the complexity O(n)? Length ofS
? length ofT
? size of an alphabet?
â vnp
Jul 30 at 20:49
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50
2
2
What is
n
in the complexity O(n)? Length of S
? length of T
? size of an alphabet?â vnp
Jul 30 at 20:49
What is
n
in the complexity O(n)? Length of S
? length of T
? size of an alphabet?â vnp
Jul 30 at 20:49
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200616%2fminimum-window-substring-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
What is
n
in the complexity O(n)? Length ofS
? length ofT
? size of an alphabet?â vnp
Jul 30 at 20:49
This is $O(n^2+n)$ complexity... I think.
â FreezePhoenix
Jul 30 at 21:20
yes. O(n^2) solution.
â NinjaG
Aug 2 at 0:50