Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int maxLength(String X, String Y, int K) {
- int n = X.length();
- int cost = 0, len = 0, maxlen = 0;
- for (int i = 0; i < n; i++) {
- cost += Math.abs(X.charAt(i) - Y.charAt(i)); // calculate the cost of changing the characters at each index i within the window.
- len++;
- while (cost > K) { // if the total cost of changing the characters within the window is more than K, shrink the window from the left to decrease the cost.
- cost -= Math.abs(X.charAt(i - len + 1) - Y.charAt(i - len + 1));
- len--;
- }
- maxlen = Math.max(maxlen, len); // update the maximum length seen so far.
- }
- return maxlen;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement