JAVA ( Sliding Window )
class Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        // Convert the problem into a min subarray problem\n        int[] di
class Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        // Convert the problem into a min subarray problem\n        int[] diff = new int[s.length()];\n        for(int i = 0; i < s.length(); ++i) {\n            int asciiS = s.charAt(i);\n            int asciiT = t.charAt(i);\n            diff[i] = Math.abs(asciiS - asciiT);\n        }\n        \n        // Now find the longest subarray <= maxCost\n        // all diff[i] >= 0 (non-negative)\n        \n        // Use sliding window?\n        int maxLen = 0;\n        int curCost = 0;\n        int start = 0;\n        \n        for(int end = 0; end < diff.length; ++end) {\n            curCost += diff[end];\n            while(curCost > maxCost) {\n                curCost -= diff[start];\n                ++start;\n            }\n            maxLen = Math.max(maxLen, end - start + 1);\n        }\n        \n        return maxLen;\n    }\n}
[]