Skip to content

Commit c2ab7c3

Browse files
authored
Merge pull request #888 from 0xff-dev/1208
Add solution and test-cases for problem 1208
2 parents 7212fed + ce6ebee commit c2ab7c3

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

leetcode/1201-1300/1208.Get-Equal-Substrings-Within-Budget/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1208.Get Equal Substrings Within Budget][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two strings `s` and `t` of the same length and an integer `maxCost`.
5+
6+
You want to change `s` to `t`. Changing the i<sup>th</sup> character of `s` to i<sup>th</sup> character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
7+
8+
Return the maximum length of a substring of `s` that can be changed to be the same as the corresponding substring of `t` with a cost less than or equal to `maxCost`. If there is no substring from `s` that can be changed to its corresponding substring from `t`, return `0`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "abcd", t = "bcdf", maxCost = 3
14+
Output: 3
15+
Explanation: "abc" of s can change to "bcd".
16+
That costs 3, so the maximum length is 3.
1317
```
1418

15-
## 题意
16-
> ...
17-
18-
## 题解
19+
**Example 2:**
1920

20-
### 思路1
21-
> ...
22-
Get Equal Substrings Within Budget
23-
```go
2421
```
22+
Input: s = "abcd", t = "cdef", maxCost = 3
23+
Output: 1
24+
Explanation: Each character in s costs 2 to change to character in t, so the maximum length is 1.
25+
```
26+
27+
**Example 3:**
2528

29+
```
30+
Input: s = "abcd", t = "acde", maxCost = 0
31+
Output: 1
32+
Explanation: You cannot make any change, so the maximum length is 1.
33+
```
2634

2735
## 结语
2836

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, t string, maxCost int) int {
4+
start, end := 0, 0
5+
ans, cur, diff := 0, 0, 0
6+
for ; end < len(s); end++ {
7+
if s[end] > t[end] {
8+
diff = int(s[end] - t[end])
9+
} else {
10+
diff = int(t[end] - s[end])
11+
}
12+
cur += diff
13+
for cur > maxCost && start <= end {
14+
if s[start] > t[start] {
15+
diff = int(s[start] - t[start])
16+
} else {
17+
diff = int(t[start] - s[start])
18+
}
19+
cur -= diff
20+
start++
21+
}
22+
ans = max(ans, end-start+1)
23+
}
24+
return ans
525
}

leetcode/1201-1300/1208.Get-Equal-Substrings-Within-Budget/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
ss, tt string
14+
maxCost int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abcd", "bcdf", 3, 3},
18+
{"TestCase2", "abcd", "cdef", 3, 1},
19+
{"TestCase3", "abcd", "acde", 0, 1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.ss, c.tt, c.maxCost)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.ss, c.tt, c.maxCost)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)