Minimum Window Substring in JavaScript

The name of the pictureThe name of the pictureThe name of the pictureClash 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"






share|improve this question

















  • 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










  • 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
















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"






share|improve this question

















  • 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










  • 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












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"






share|improve this question













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"








share|improve this question












share|improve this question




share|improve this question








edited Jul 31 at 4:14









Jamal♦

30.1k11114225




30.1k11114225









asked Jul 30 at 20:19









NinjaG

634221




634221







  • 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










  • 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




    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











  • 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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation