Skip to content

feat: add solutions to lc problem: No.1208 #2932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 28, 2024
Prev Previous commit
Next Next commit
Update Solution2.java
  • Loading branch information
yanglbme authored May 28, 2024
commit 362dd53ce1651ed81fe4f11ab42fb457a2add1ce
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int sum = 0;
int ans = 0;
for (int i = 0, j = 0; i < n; ++i) {
sum += Math.abs(s.charAt(i) - t.charAt(i));
while (sum > maxCost) {
sum -= Math.abs(s.charAt(j) - t.charAt(j));
++j;
int ans = 0, cost = 0;
for (int l = 0, r = 0; r < n; ++r) {
cost += Math.abs(s.charAt(r) - t.charAt(r));
while (cost > maxCost) {
cost -= Math.abs(s.charAt(l) - t.charAt(l));
++l;
}
ans = Math.max(ans, i - j + 1);
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
}