diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md index d0ede68aadf0d..eb097e9454c46 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md @@ -222,6 +222,41 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const n = s.length; + const f = Array(n + 1).fill(0); + + for (let i = 0; i < n; i++) { + f[i + 1] = f[i] + Math.abs(s.charCodeAt(i) - t.charCodeAt(i)); + } + + const check = (x: number): boolean => { + for (let i = 0; i + x - 1 < n; i++) { + if (f[i + x] - f[i] <= maxCost) { + return true; + } + } + return false; + }; + + let l = 0, + r = n; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + + return l; +} +``` + @@ -230,11 +265,11 @@ func abs(x int) int { ### 方法二:双指针 -我们可以维护两个指针 $j$ 和 $i$,初始时 $i = j = 0$;维护一个变量 $sum$,表示下标区间 $[i,..j]$ 之间的 ASCII 码值的差的绝对值之和。在每一步中,我们将 $i$ 向右移动一位,然后更新 $sum = sum + |s[i] - t[i]|$。如果 $sum \gt maxCost$,那么我们就循环将指针 $j$ 向右移动,并且在移动过程中不断减少 $sum$ 的值,直到 $sum \leq maxCost$。然后我们更新答案,即 $ans = \max(ans, i - j + 1)$。 +我们可以维护两个指针 $l$ 和 $r$,初始时 $l = r = 0$;维护一个变量 $\text{cost}$,表示下标区间 $[l,..r]$ 之间的 ASCII 码值的差的绝对值之和。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\text{cost} = \text{cost} + |s[r] - t[r]|$。如果 $\text{cost} \gt \text{maxCost}$,那么我们就循环将 $l$ 向右移动一位,并且减少 $\text{cost}$ 的值,直到 $\text{cost} \leq \text{maxCost}$。然后我们更新答案,即 $\text{ans} = \max(\text{ans}, r - l + 1)$。 最后返回答案即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 @@ -244,14 +279,13 @@ func abs(x int) int { class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: n = len(s) - sum = j = 0 - ans = 0 - for i in range(n): - sum += abs(ord(s[i]) - ord(t[i])) - while sum > maxCost: - sum -= abs(ord(s[j]) - ord(t[j])) - j += 1 - ans = max(ans, i - j + 1) + ans = cost = l = 0 + for r in range(n): + cost += abs(ord(s[r]) - ord(t[r])) + while cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + ans = max(ans, r - l + 1) return ans ``` @@ -261,15 +295,14 @@ class Solution: 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; } @@ -282,15 +315,15 @@ class Solution { class Solution { public: int equalSubstring(string s, string t, int maxCost) { - int n = s.size(); - int ans = 0, sum = 0; - for (int i = 0, j = 0; i < n; ++i) { - sum += abs(s[i] - t[i]); - while (sum > maxCost) { - sum -= abs(s[j] - t[j]); - ++j; + int n = s.length(); + int ans = 0, cost = 0; + for (int l = 0, r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + while (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; } - ans = max(ans, i - j + 1); + ans = max(ans, r - l + 1); } return ans; } @@ -301,15 +334,13 @@ public: ```go func equalSubstring(s string, t string, maxCost int) (ans int) { - var sum, j int - for i := range s { - sum += abs(int(s[i]) - int(t[i])) - for ; sum > maxCost; j++ { - sum -= abs(int(s[j]) - int(t[j])) - } - if ans < i-j+1 { - ans = i - j + 1 + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + for ; cost > maxCost; l++ { + cost -= abs(int(s[l]) - int(t[l])) } + ans = max(ans, r-l+1) } return } @@ -322,6 +353,138 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let ans = 0, + cost = 0; + for (let l = 0, r = 0; r < n; ++r) { + cost += getCost(r); + while (cost > maxCost) { + cost -= getCost(l++); + } + ans = Math.max(ans, r - l + 1); + } + return ans; +} +``` + + + + + + + +### 方法三:双指针的另一种写法 + +在方法二中,双指针维护的区间可能变短,也可能变长,由于题目只需要求出最大长度,我们可以维护一个单调变长的区间。 + +具体地,我们用两个指针 $l$ 和 $r$ 指向区间的左右端点,初始时 $l = r = 0$。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\text{cost} = \text{cost} + |s[r] - t[r]|$。如果 $\text{cost} \gt \text{maxCost}$,那么我们就将 $l$ 向右移动一位,并且减少 $\text{cost}$ 的值。 + +最后返回 $n - l$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def equalSubstring(self, s: str, t: str, maxCost: int) -> int: + cost = l = 0 + for a, b in zip(s, t): + cost += abs(ord(a) - ord(b)) + if cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + return len(s) - l +``` + +#### Java + +```java +class Solution { + public int equalSubstring(String s, String t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += Math.abs(s.charAt(r) - t.charAt(r)); + if (cost > maxCost) { + cost -= Math.abs(s.charAt(l) - t.charAt(l)); + ++l; + } + } + return n - l; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + if (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; + } + } + return n - l; + } +}; +``` + +#### Go + +```go +func equalSubstring(s string, t string, maxCost int) int { + n := len(s) + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + if cost > maxCost { + cost -= abs(int(s[l]) - int(t[l])) + l++ + } + } + return n - l +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let cost = 0; + let l = 0; + for (let r = 0; r < n; ++r) { + cost += getCost(r); + if (cost > maxCost) { + cost -= getCost(l++); + } + } + return n - l; +} +``` + diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md index ee7ed2c0ce415..4e1b62096976b 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md @@ -219,6 +219,41 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const n = s.length; + const f = Array(n + 1).fill(0); + + for (let i = 0; i < n; i++) { + f[i + 1] = f[i] + Math.abs(s.charCodeAt(i) - t.charCodeAt(i)); + } + + const check = (x: number): boolean => { + for (let i = 0; i + x - 1 < n; i++) { + if (f[i + x] - f[i] <= maxCost) { + return true; + } + } + return false; + }; + + let l = 0, + r = n; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + + return l; +} +``` + @@ -227,11 +262,11 @@ func abs(x int) int { ### Solution 2: Two Pointers -We can maintain two pointers $j$ and $i$, initially $i = j = 0$; maintain a variable $sum$, representing the sum of the absolute differences of ASCII values in the index interval $[i,..j]$. In each step, we move $i$ to the right by one position, then update $sum = sum + |s[i] - t[i]|$. If $sum \gt maxCost$, then we move the pointer $j$ to the right in a loop, and continuously reduce the value of $sum$ during the moving process until $sum \leq maxCost$. Then we update the answer, i.e., $ans = \max(ans, i - j + 1)$. +We can maintain two pointers $l$ and $r$, initially $l = r = 0$; maintain a variable $\text{cost}$, which represents the sum of the absolute values of the ASCII code differences in the index interval $[l,..r]$. In each step, we move $r$ to the right by one position, then update $\text{cost} = \text{cost} + |s[r] - t[r]|$. If $\text{cost} \gt \text{maxCost}$, then we loop to move $l$ to the right by one position, and decrease the value of $\text{cost}$, until $\text{cost} \leq \text{maxCost}$. Then we update the answer, that is, $\text{ans} = \max(\text{ans}, r - l + 1)$. Finally, return the answer. -The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of string $s$. +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. @@ -241,14 +276,13 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: n = len(s) - sum = j = 0 - ans = 0 - for i in range(n): - sum += abs(ord(s[i]) - ord(t[i])) - while sum > maxCost: - sum -= abs(ord(s[j]) - ord(t[j])) - j += 1 - ans = max(ans, i - j + 1) + ans = cost = l = 0 + for r in range(n): + cost += abs(ord(s[r]) - ord(t[r])) + while cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + ans = max(ans, r - l + 1) return ans ``` @@ -258,15 +292,14 @@ class Solution: 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; } @@ -279,15 +312,15 @@ class Solution { class Solution { public: int equalSubstring(string s, string t, int maxCost) { - int n = s.size(); - int ans = 0, sum = 0; - for (int i = 0, j = 0; i < n; ++i) { - sum += abs(s[i] - t[i]); - while (sum > maxCost) { - sum -= abs(s[j] - t[j]); - ++j; + int n = s.length(); + int ans = 0, cost = 0; + for (int l = 0, r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + while (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; } - ans = max(ans, i - j + 1); + ans = max(ans, r - l + 1); } return ans; } @@ -298,15 +331,13 @@ public: ```go func equalSubstring(s string, t string, maxCost int) (ans int) { - var sum, j int - for i := range s { - sum += abs(int(s[i]) - int(t[i])) - for ; sum > maxCost; j++ { - sum -= abs(int(s[j]) - int(t[j])) - } - if ans < i-j+1 { - ans = i - j + 1 + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + for ; cost > maxCost; l++ { + cost -= abs(int(s[l]) - int(t[l])) } + ans = max(ans, r-l+1) } return } @@ -319,6 +350,138 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let ans = 0, + cost = 0; + for (let l = 0, r = 0; r < n; ++r) { + cost += getCost(r); + while (cost > maxCost) { + cost -= getCost(l++); + } + ans = Math.max(ans, r - l + 1); + } + return ans; +} +``` + + + + + + + +### Solution 3: Another Way of Using Two Pointers + +In Solution 2, the interval maintained by the two pointers may become shorter or longer. Since the problem only requires the maximum length, we can maintain a monotonically increasing interval. + +Specifically, we use two pointers $l$ and $r$ to point to the left and right endpoints of the interval, initially $l = r = 0$. In each step, we move $r$ to the right by one position, then update $\text{cost} = \text{cost} + |s[r] - t[r]|$. If $\text{cost} \gt \text{maxCost}$, then we move $l$ to the right by one position, and decrease the value of $\text{cost}$. + +Finally, return $n - l$. + +The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the string $s$. + + + +#### Python3 + +```python +class Solution: + def equalSubstring(self, s: str, t: str, maxCost: int) -> int: + cost = l = 0 + for a, b in zip(s, t): + cost += abs(ord(a) - ord(b)) + if cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + return len(s) - l +``` + +#### Java + +```java +class Solution { + public int equalSubstring(String s, String t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += Math.abs(s.charAt(r) - t.charAt(r)); + if (cost > maxCost) { + cost -= Math.abs(s.charAt(l) - t.charAt(l)); + ++l; + } + } + return n - l; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + if (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; + } + } + return n - l; + } +}; +``` + +#### Go + +```go +func equalSubstring(s string, t string, maxCost int) int { + n := len(s) + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + if cost > maxCost { + cost -= abs(int(s[l]) - int(t[l])) + l++ + } + } + return n - l +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + +#### TypeScript + +```ts +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let cost = 0; + let l = 0; + for (let r = 0; r < n; ++r) { + cost += getCost(r); + if (cost > maxCost) { + cost -= getCost(l++); + } + } + return n - l; +} +``` + diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.ts b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.ts new file mode 100644 index 0000000000000..e1688f4a7cb61 --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.ts @@ -0,0 +1,30 @@ +function equalSubstring(s: string, t: string, maxCost: number): number { + const n = s.length; + const f = Array(n + 1).fill(0); + + for (let i = 0; i < n; i++) { + f[i + 1] = f[i] + Math.abs(s.charCodeAt(i) - t.charCodeAt(i)); + } + + const check = (x: number): boolean => { + for (let i = 0; i + x - 1 < n; i++) { + if (f[i + x] - f[i] <= maxCost) { + return true; + } + } + return false; + }; + + let l = 0, + r = n; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + + return l; +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.cpp b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.cpp index a0e0f72698fd4..113e5613e2f1a 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.cpp +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.cpp @@ -1,16 +1,16 @@ class Solution { public: int equalSubstring(string s, string t, int maxCost) { - int n = s.size(); - int ans = 0, sum = 0; - for (int i = 0, j = 0; i < n; ++i) { - sum += abs(s[i] - t[i]); - while (sum > maxCost) { - sum -= abs(s[j] - t[j]); - ++j; + int n = s.length(); + int ans = 0, cost = 0; + for (int l = 0, r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + while (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; } - ans = max(ans, i - j + 1); + ans = max(ans, r - l + 1); } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.go b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.go index 71c258812a475..bf27681f906d9 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.go +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.go @@ -1,13 +1,11 @@ func equalSubstring(s string, t string, maxCost int) (ans int) { - var sum, j int - for i := range s { - sum += abs(int(s[i]) - int(t[i])) - for ; sum > maxCost; j++ { - sum -= abs(int(s[j]) - int(t[j])) - } - if ans < i-j+1 { - ans = i - j + 1 + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + for ; cost > maxCost; l++ { + cost -= abs(int(s[l]) - int(t[l])) } + ans = max(ans, r-l+1) } return } @@ -17,4 +15,4 @@ func abs(x int) int { return -x } return x -} \ No newline at end of file +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.java b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.java index 032910aacb4f8..324c76e20d0d6 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.java +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.py b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.py index 0abac53fc8bb8..1556cc93c0661 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.py +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.py @@ -1,12 +1,11 @@ class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: n = len(s) - sum = j = 0 - ans = 0 - for i in range(n): - sum += abs(ord(s[i]) - ord(t[i])) - while sum > maxCost: - sum -= abs(ord(s[j]) - ord(t[j])) - j += 1 - ans = max(ans, i - j + 1) + ans = cost = l = 0 + for r in range(n): + cost += abs(ord(s[r]) - ord(t[r])) + while cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + ans = max(ans, r - l + 1) return ans diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts new file mode 100644 index 0000000000000..6d2bb8f66436a --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts @@ -0,0 +1,14 @@ +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let ans = 0, + cost = 0; + for (let l = 0, r = 0; r < n; ++r) { + cost += getCost(r); + while (cost > maxCost) { + cost -= getCost(l++); + } + ans = Math.max(ans, r - l + 1); + } + return ans; +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.cpp b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.cpp new file mode 100644 index 0000000000000..fc99a7c7b3814 --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += abs(s[r] - t[r]); + if (cost > maxCost) { + cost -= abs(s[l] - t[l]); + ++l; + } + } + return n - l; + } +}; diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.go b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.go new file mode 100644 index 0000000000000..c9b3b2f026cda --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.go @@ -0,0 +1,19 @@ +func equalSubstring(s string, t string, maxCost int) int { + n := len(s) + var cost, l int + for r := range s { + cost += abs(int(s[r]) - int(t[r])) + if cost > maxCost { + cost -= abs(int(s[l]) - int(t[l])) + l++ + } + } + return n - l +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.java b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.java new file mode 100644 index 0000000000000..45135e06aa140 --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.java @@ -0,0 +1,14 @@ +class Solution { + public int equalSubstring(String s, String t, int maxCost) { + int n = s.length(); + int cost = 0, l = 0; + for (int r = 0; r < n; ++r) { + cost += Math.abs(s.charAt(r) - t.charAt(r)); + if (cost > maxCost) { + cost -= Math.abs(s.charAt(l) - t.charAt(l)); + ++l; + } + } + return n - l; + } +} diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.py b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.py new file mode 100644 index 0000000000000..7104c5940730d --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.py @@ -0,0 +1,9 @@ +class Solution: + def equalSubstring(self, s: str, t: str, maxCost: int) -> int: + cost = l = 0 + for a, b in zip(s, t): + cost += abs(ord(a) - ord(b)) + if cost > maxCost: + cost -= abs(ord(s[l]) - ord(t[l])) + l += 1 + return len(s) - l diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.ts b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.ts new file mode 100644 index 0000000000000..1878c55275a79 --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.ts @@ -0,0 +1,13 @@ +function equalSubstring(s: string, t: string, maxCost: number): number { + const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0)); + const n = s.length; + let cost = 0; + let l = 0; + for (let r = 0; r < n; ++r) { + cost += getCost(r); + if (cost > maxCost) { + cost -= getCost(l++); + } + } + return n - l; +}