Find the smallest window in a string containing all characters of another string | Python solution ; Starting index of ans ; Answer Length of ans ; creating map ; References of Window ; Traversing the window ; Calculating ; Condition matching ; calculating answer . ; Sliding I Calculation for removing I ; Driver code
def smallestWindow ( s , p ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if n < len ( p ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mp = [ 0 ] * 256 NEW_LINE start = 0 NEW_LINE ans = n + 1 NEW_LINE cnt = 0 NEW_LINE for i in p : NEW_LINE INDENT mp [ ord ( i ) ] += 1 NEW_LINE if mp [ ord ( i ) ] == 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT j = 0 NEW_LINE i = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT mp [ ord ( s [ j ] ) ] -= 1 NEW_LINE if mp [ ord ( s [ j ] ) ] == 0 : NEW_LINE INDENT cnt -= 1 NEW_LINE while cnt == 0 : NEW_LINE INDENT if ans > j - i + 1 : NEW_LINE INDENT ans = j - i + 1 NEW_LINE start = i NEW_LINE DEDENT mp [ ord ( s [ i ] ) ] += 1 NEW_LINE if mp [ ord ( s [ i ] ) ] > 0 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT if ans > n : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT return s [ start : start + ans ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ADOBECODEBANC " NEW_LINE p = " ABC " NEW_LINE result = smallestWindow ( s , p ) NEW_LINE print ( " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : " , result ) NEW_LINE DEDENT