Find the smallest window in a string containing all characters of another string | Function ; Answer 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
using System ; class GFG { static String Minimum_Window ( char [ ] s , char [ ] t ) { int [ ] m = new int [ 256 ] ; int ans = int . MaxValue ; int start = 0 ; int count = 0 , i = 0 ; for ( i = 0 ; i < t . Length ; i ++ ) { if ( m [ t [ i ] ] == 0 ) count ++ ; m [ t [ i ] ] ++ ; } i = 0 ; int j = 0 ; while ( j < s . Length ) { m [ s [ j ] ] -- ; if ( m [ s [ j ] ] == 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 ] ] ++ ; if ( m [ s [ i ] ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != int . MaxValue ) return String . Join ( " " , s ) . Substring ( start , ans ) ; else return " - 1" ; } public static void Main ( String [ ] args ) { String s = " ADOBECODEBANC " ; String t = " ABC " ; Console . Write ( " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : ▁ " ) ; Console . Write ( Minimum_Window ( s . ToCharArray ( ) , t . ToCharArray ( ) ) ) ; } }