package leetcode;

/**
 * https://leetcode.com/problems/get-equal-substrings-within-budget/
 */
public class Problem1208 {
    public int equalSubstring(String s, String t, int maxCost) {
        int size = s.length();
        int prev = 0;
        int answer = 0;
        int cost = maxCost;
        for (int cur = 0; cur < size; cur++) {
            int diff = Math.abs(s.charAt(cur) - t.charAt(cur));
            if (diff > maxCost) {
                cost = maxCost;
                answer = Math.max(answer, cur - prev);
                prev = cur + 1;
                continue;
            }
            cost -= diff;
            if (cost < 0) {
                cost = maxCost - diff;
                answer = Math.max(answer, cur - prev);
                prev = cur;
            }
        }
        if (cost >= 0) {
            answer = Math.max(answer, size - prev);
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1208 prob = new Problem1208();
//        System.out.println(prob.equalSubstring("abcd", "bcdf", 3)); // 3
//        System.out.println(prob.equalSubstring("abaace", "bcxbdf", 3)); // 3
//        System.out.println(prob.equalSubstring("ababace", "bcxxbdf", 3)); // 3
//        System.out.println(prob.equalSubstring("abcd", "cdef", 3)); // 1
//        System.out.println(prob.equalSubstring("abcd", "acde", 0)); // 1
//        System.out.println(prob.equalSubstring("abcd", "bxdf", 2)); // 1
//        System.out.println(prob.equalSubstring("abcd", "bcde", 5)); // 4
//        System.out.println(prob.equalSubstring("abcd", "cdef", 1)); // 0
        System.out.println(prob.equalSubstring("ujteygggjwxnfl", "nstsenrzttikoy", 43)); // 5
    }
}

@@ -648,47 +648,8 @@
 ) %7B%0A
-                cost = maxCost - diff;%0A
     
@@ -719,119 +719,57 @@
     
-prev = cur;%0A            %7D%0A        %7D%0A        if (cost %3E= 0) %7B%0A            answer = Math.max(answer, size - 
+cost += Math.abs(s.charAt(prev) - t.charAt(
 prev)
+)
 ;%0A  
@@ -774,26 +774,24 @@
         
-%7D%0A
         
 return a
@@ -786,824 +786,149 @@
     
-return answer
+prev++
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1208 prob = new Problem1208();%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22bcdf%22, 3)); // 3%0A//        System.out.println(prob.equalSubstring(%22abaace%22, %22bcxbdf%22, 3)); // 3%0A//        System.out.println(prob.equalSubstring(%22ababace%22, %22bcxxbdf%22, 3)); // 3%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22cdef%22, 3)); // 1%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22acde%22, 0)); // 1%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22bxdf%22, 2)); // 1%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22bcde%22, 5)); // 4%0A//        System.out.println(prob.equalSubstring(%22abcd%22, %22cdef%22, 1)); // 0%0A        System.out.println(prob.equalSubstring(%22ujteygggjwxnfl%22, %22nstsenrzttikoy%22, 43)); // 5
+    %7D%0A        %7D%0A        if (cost %3E= 0) %7B%0A            answer = Math.max(answer, size - prev);%0A        %7D%0A        return answer;
 %0A   

Solve problem 1208