From a48fd87135b7d9ab98af5c51a9887bccf93ad037 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 28 May 2024 00:16:51 +0300 Subject: [PATCH 01/15] feat: add TypeScript solution to lc problem: No.1208 --- .../README.md | 22 +++++++++++++++++++ .../README_EN.md | 22 +++++++++++++++++++ .../Solution2.ts | 17 ++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts 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..a8a728145aa20 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 @@ -322,6 +322,28 @@ 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 res = 0; + let l = 0; + let r = -1; + let cost = 0; + + while (++r < n) { + cost += getCost(r); + + if (cost <= maxCost) res = Math.max(res, r - l + 1); + else cost -= getCost(l++); + } + + return res; +} +``` + 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..2b39fbbfeeeb3 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 @@ -319,6 +319,28 @@ 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 res = 0; + let l = 0; + let r = -1; + let cost = 0; + + while (++r < n) { + cost += getCost(r); + + if (cost <= maxCost) res = Math.max(res, r - l + 1); + else cost -= getCost(l++); + } + + return res; +} +``` + 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..c6df052cb5bca --- /dev/null +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts @@ -0,0 +1,17 @@ +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 res = 0; + let l = 0; + let r = -1; + let cost = 0; + + while (++r < n) { + cost += getCost(r); + + if (cost <= maxCost) res = Math.max(res, r - l + 1); + else cost -= getCost(l++); + } + + return res; +} From f21478daae56f4b01c430dc1cac76373b7c79125 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:42:05 +0800 Subject: [PATCH 02/15] Update README.md --- .../README.md | 228 ++++++++++++++---- 1 file changed, 185 insertions(+), 43 deletions(-) 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 a8a728145aa20..b19151fb880de 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 } @@ -328,19 +359,129 @@ func abs(x int) int { 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 res = 0; - let l = 0; - let r = -1; - let cost = 0; - - while (++r < n) { + 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 - if (cost <= maxCost) res = Math.max(res, r - l + 1); - else cost -= getCost(l++); +```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 +} - return res; +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; } ``` @@ -349,3 +490,4 @@ function equalSubstring(s: string, t: string, maxCost: number): number { + From 104eedf3c090badf8a408ba009387cc783345558 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:42:39 +0800 Subject: [PATCH 03/15] Update README_EN.md --- .../README_EN.md | 227 ++++++++++++++---- 1 file changed, 184 insertions(+), 43 deletions(-) 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 2b39fbbfeeeb3..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 } @@ -325,19 +356,129 @@ func abs(x int) int { 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 res = 0; - let l = 0; - let r = -1; - let cost = 0; - - while (++r < n) { + 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 - if (cost <= maxCost) res = Math.max(res, r - l + 1); - else cost -= getCost(l++); +```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++ - return res; +```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; } ``` From a0a97507a054eaffc8463eaa2d04838b8c0061fa Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:43:21 +0800 Subject: [PATCH 04/15] Create Solution.ts --- .../Solution.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.ts 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; +} From bc40a7892a4dcd7484e688a012abaf8d844c67c5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:43:35 +0800 Subject: [PATCH 05/15] Update Solution2.py --- .../Solution2.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 From 362dd53ce1651ed81fe4f11ab42fb457a2add1ce Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:43:44 +0800 Subject: [PATCH 06/15] Update Solution2.java --- .../Solution2.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 +} From 907b9fe30c47b6b3288d0f394390bbec2c307ee6 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:43:54 +0800 Subject: [PATCH 07/15] Update Solution2.cpp --- .../Solution2.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 +}; From 8053c4d60d49936f6ce9f0030c58bd86883e3007 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:44:03 +0800 Subject: [PATCH 08/15] Update Solution2.go --- .../Solution2.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) 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 +} From b53a1fde5cf10c023dcbb0b895a08ea81af02c5b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:44:12 +0800 Subject: [PATCH 09/15] Update Solution2.ts --- .../Solution2.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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 index c6df052cb5bca..6d2bb8f66436a 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution2.ts @@ -1,17 +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 res = 0; - let l = 0; - let r = -1; - let cost = 0; - - while (++r < n) { + let ans = 0, + cost = 0; + for (let l = 0, r = 0; r < n; ++r) { cost += getCost(r); - - if (cost <= maxCost) res = Math.max(res, r - l + 1); - else cost -= getCost(l++); + while (cost > maxCost) { + cost -= getCost(l++); + } + ans = Math.max(ans, r - l + 1); } - - return res; + return ans; } From 45d838e8e391056653908c7099394874743d77b7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:44:42 +0800 Subject: [PATCH 10/15] Create Solution3.py --- .../1208.Get Equal Substrings Within Budget/Solution3.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.py 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 From 93a1da495c024a3c570c85394acec9a5be37242e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:44:57 +0800 Subject: [PATCH 11/15] Create Solution3.java --- .../Solution3.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.java 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; + } +} From de2e44403614a2d6423d8f3a4b980623d65c8212 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:45:09 +0800 Subject: [PATCH 12/15] Create Solution3.cpp --- .../Solution3.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.cpp 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; + } +}; From 3c1e86f584d79ac5d8b5c791539656018183db47 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:45:20 +0800 Subject: [PATCH 13/15] Create Solution3.go --- .../Solution3.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.go 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 +} From 3ed9936cb1a115147879f905d2ec80d915d817a7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 28 May 2024 19:45:33 +0800 Subject: [PATCH 14/15] Create Solution3.ts --- .../Solution3.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution3.ts 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; +} From 562f957d47675e0344190001c387a08d790e435b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 28 May 2024 11:47:07 +0000 Subject: [PATCH 15/15] style: format code and docs with prettier --- .../1200-1299/1208.Get Equal Substrings Within Budget/README.md | 1 - 1 file changed, 1 deletion(-) 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 b19151fb880de..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 @@ -490,4 +490,3 @@ function equalSubstring(s: string, t: string, maxCost: number): number { -