A visualized first principles approach for better understanding
Heres a visualization techinque that might help you get a better idea of the solution. \n\nImagine the strings (henceforth S and T) as a long chain of character
Heres a visualization techinque that might help you get a better idea of the solution. \n\nImagine the strings (henceforth **S** and **T**) as a long chain of characters\n\nFirst we will build a tool that will help us extract information about the target string **T**\n\nWe will extract 2 pieces of info from **T** - \nfirst how many characters there are in T and what is the frequency of each character. \n\nWe focus on these 2 pieces of information because any substring in S that must contain T should satisfy 2 criteria: a. Have all the characters in T, b. Match the character frequency of T \n\n![image](https://assets.leetcode.com/users/images/820ea541-2611-43a1-b5dd-7c1e3dbcd3db_1595282693.3528335.png)\n\nSo Rem is the total number of characters remaining, and each characters frequency is the count of each char that is required. \n\nNext we will use this "tool" and run a sliding window over **S** to get to point where REM is 0. The points where REM is 0 are our windows where we have found all the characters that are present in T and match the same frequency. \n\nWe will continue expanding our window as long as REM is postive.\nAt a step when Rem is <=0 we will start shrinking our window from the start until REM stays below or equal to 0. This will help us find our minimum length substring \n\n\n\nIt is important to note that the variable REM is changed **ONLY** when we see a character in **S** that is present in our tool. Then it can be that either: \n\na. We are expanding our window and the frequency of the matching character is postive in our tool. In this case required character frequency and REM are decreased **OR**\nb. We are shirinking our window and the frequency of the matching character is negative in our tool. In this case character frequency is increased and REM is increased. \n \n A run of the two steps above is shown below: \n \n ![image](https://assets.leetcode.com/users/images/6ebd8d03-49a4-4d7f-9f56-e5e1eb802f76_1595286128.92845.png)\n ![image](https://assets.leetcode.com/users/images/9ba7034b-a161-4f61-be1b-72ee02fe0b1a_1595286137.6609843.png)\n ![image](https://assets.leetcode.com/users/images/25e7dd70-2fef-411b-b1e2-20d768333bdb_1595286143.7318404.png)\n ![image](https://assets.leetcode.com/users/images/15297ab5-1fa0-4343-a086-ddcebb07896d_1595286150.1999655.png)\n![image](https://assets.leetcode.com/users/images/0d3d1ff7-b774-4682-b363-d3ab0127ae1a_1595286158.0941873.png)\n![image](https://assets.leetcode.com/users/images/a4acb697-8e09-410f-a251-a6526b44161b_1595286163.2758465.png)\n![image](https://assets.leetcode.com/users/images/9d52686a-2a67-47fe-b73a-023a4e6807fa_1595286169.876245.png)\n![image](https://assets.leetcode.com/users/images/eb854cfe-65e3-4ed8-bf22-6d9336345502_1595286176.810449.png)\n![image](https://assets.leetcode.com/users/images/2c079361-d3bc-4118-9709-c207a514ec11_1595286181.926014.png)\n![image](https://assets.leetcode.com/users/images/115728db-9b24-4050-890a-fb2fed90e207_1595286188.157475.png)\n![image](https://assets.leetcode.com/users/images/9d478daa-bf71-4e96-88cc-090d7e9b96d4_1595286193.4163616.png)\n![image](https://assets.leetcode.com/users/images/5cc7c448-6b32-4c6a-833d-b62a847d4ac9_1595286198.9250457.png)\n![image](https://assets.leetcode.com/users/images/a3c3a564-6375-4c4c-9ed3-ef7fd7220455_1595286205.0798516.png)\n![image](https://assets.leetcode.com/users/images/d84c131e-318e-41a9-9f58-2f1564c93669_1595286211.780769.png)\n![image](https://assets.leetcode.com/users/images/599bd4a0-fd18-4367-8305-7bc7b83b38be_1595286217.9472275.png)\n\nHeres a corresponding AC solution in golang\n```\nfunc minWindow(s string, t string) string {\n    rem := 0\n    counter := make(map[byte]int)\n    for i := range t {\n        rem++\n        counter[t[i]]++\n    }\n    if rem > len(s) {\n        return ""\n    }\n    var ret = string(make([]byte, len(s)))\n    start, end := 0,0\n    for end < len(s){\n        if v,ok := counter[s[end]] ;ok {\n            if v>0 {\n                rem--\n            }\n            counter[s[end]]--\n        }\n        for rem <= 0 {\n            if len(ret) >= len(s[start:end+1]) {\n                ret = s[start:end+1]\n            }\n            if _,ok := counter[s[start]] ;ok {\n                counter[s[start]]++\n                if counter[s[start]] > 0 {\n                    rem++    \n                }  \n            }\n            start++\n        }\n        end++\n    }\n    \n    if ret == string(make([]byte, len(s))) {\n        return ""\n    }\n    return ret\n}\n\n\n\n\n\n\n\n\n\n\n
['Go']