Untitled
unknown
plain_text
a year ago
1.3 kB
6
Indexable
/T.C : O(2n)
//S.C : O(1)
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int maxLen = 0;
int currCost = 0;
int i = 0, j = 0;
while (j < n) {
currCost += Math.abs(s.charAt(j) - t.charAt(j));
while (currCost > maxCost) {
currCost -= Math.abs(s.charAt(i) - t.charAt(i));
i++;
}
maxLen = Math.max(maxLen, j - i + 1);
j++;
}
return maxLen;
}
}
TC : O(N) SC ; O(1)
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int maxLen = 0;
int currCost = 0;
int i = 0, j = 0;
while (j < n) {
currCost += Math.abs(s.charAt(j) - t.charAt(j));
if (currCost > maxCost) {
currCost -= Math.abs(s.charAt(i) - t.charAt(i));
i++;
}
if (currCost <= maxCost)
maxLen = Math.max(maxLen, j - i + 1);
j++;
}
return maxLen;
}
}Editor is loading...
Leave a Comment