Java O(N) Soln | Beats 99.3% in TC | Good Readability | Beginner's Friendly
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity : O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int[] cost = new int[s.length()];\n        for (int i=0 ; i<cost.length ; i++){\n            int temp = s.charAt(i) - t.charAt(i);\n            if (temp<0){\n                temp = -temp;\n            }\n            cost[i] = temp;\n        }\n\n        if (cost.length == 1){\n            if (cost[0] <= maxCost){\n                return 1;\n            }\n            return 0;\n        }\n\n        int start = 0;\n        int currCost = 0;\n        int maxLength = 0;\n\n        for (int end = 0; end < cost.length; end++) {\n            currCost += cost[end];\n\n            while (currCost > maxCost && start <= end) {\n                currCost -= cost[start];\n                start++;\n            }\n\n            maxLength = Math.max(maxLength, end - start + 1);\n        }\n\n        return maxLength;\n    }\n}\n```\n# ---------------------------------------------------------\n# .......................Don\'t forget to upvote.......................\n# ---------------------------------------------------------
['String', 'Sliding Window', 'Java']