Sliding Window Thinking Process
For example\n\ne.g. S = "ADOBECODEBANC", T = "ABC"\n          ADOBEC \n\t         BECODEBA \n               CODEBA\n                   BANC\nThe substrings abov
> For example\n```\ne.g. S = "ADOBECODEBANC", T = "ABC"\n          ADOBEC \n\t         BECODEBA \n               CODEBA\n                   BANC\nThe substrings above are candidates for the result.\n```\n> In Brute Force, \n```\nfor left in [0: sLen - 1]\n  for right in [left*: sLen - 1]\n    if (canCover()) \n\t  update minLen and minStr\n\t  break\n```\n> After observation, there is no need to reset right to left here*\n> If canCover, we move left forward until !canCover, we move right forward\n```\nfor left in [0: sLen - 1]\n  while (right < sLen && !canCover()) \n\tright++\n  update minLen and minStr\n  left++\n```\n> So we use the Sliding Window technique to generate these candidates. \n****\n```\n    private int[] charToFreqT; // Map char to freq in t\n    \n    public String minWindow(String s, String t) {\n        // Corner case\n        if (s.isEmpty() || t.isEmpty()) return "";\n        \n        buildCharToFreqT(t);\n        \n        int sLen = s.length(), minLen = Integer.MAX_VALUE, left = 0, right = 0;\n        String minStr = ""; // Final result\n        \n        charToFreqT[s.charAt(0)]--;\n        \n        while (left <= right) { \n            while (right < sLen && !canCover()) { \n                right++;\n                if (right < sLen) charToFreqT[s.charAt(right)]--;\n            }\n            if (right == sLen) break;\n            // Update min by current window [left, right]\n            if (right - left + 1 < minLen) {\n                minLen = right - left + 1;\n                minStr = s.substring(left, right + 1);\n            }\n            charToFreqT[s.charAt(left)]++;\n            left++;\n        }\n        \n        return minStr;\n    }\n    \n    private void buildCharToFreqT(String t) {\n        charToFreqT = new int[256];\n        for (char ch : t.toCharArray()) {\n            charToFreqT[ch]++;\n        }\n    }\n    \n    private boolean canCover() {\n        // For each key in charToFreqT, charToFreqS contains and value >= T\n        for (int freq : charToFreqT) {\n            if (freq > 0) return false;\n        }\n        return true;\n    }\n```\n**(\u4EBA \u2022\u0348\u1D17\u2022\u0348)** Thanks for voting!
[]