Find the smallest window in a string containing all characters of another string | Function ; Length of ans ; Starting index of ans ; Creating map ; References of Window ; Traversing the window ; Calculations ; Condition matching ; Sorting ans ; Sliding I Calculation for removing I ; Driver code
function Minimum_Window ( s , t ) { let m = new Array ( 256 ) ; for ( let i = 0 ; i < 256 ; i ++ ) { m [ i ] = 0 ; } let ans = Number . MAX_VALUE ; let start = 0 ; let count = 0 ; for ( let i = 0 ; i < t . length ; i ++ ) { if ( m [ t [ i ] . charCodeAt ( 0 ) ] == 0 ) count ++ ; m [ t [ i ] . charCodeAt ( 0 ) ] ++ ; } let i = 0 ; let j = 0 ; while ( j < s . length ) { m [ s [ j ] . charCodeAt ( 0 ) ] -- ; if ( m [ s [ j ] . charCodeAt ( 0 ) ] == 0 ) count -- ; if ( count == 0 ) { while ( count == 0 ) { if ( ans > j - i + 1 ) { ans = Math . min ( ans , j - i + 1 ) ; start = i ; } m [ s [ i ] . charCodeAt ( 0 ) ] ++ ; if ( m [ s [ i ] . charCodeAt ( 0 ) ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != Number . MAX_VALUE ) return ( s ) . join ( " " ) . substring ( start , ( start + ans ) ) ; else return " " ; } let s = " " ; let t = " " ; document . write ( " " + " " ) ; document . write ( Minimum_Window ( s . split ( " " ) , t . split ( " " ) ) ) ;