Skip to content

Commit

Permalink
Merge pull request #888 from 0xff-dev/1208
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1208
  • Loading branch information
6boris committed Jun 6, 2024
2 parents 7212fed + ce6ebee commit c2ab7c3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [1208.Get Equal Substrings Within Budget][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given two strings `s` and `t` of the same length and an integer `maxCost`.

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).

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`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd".
That costs 3, so the maximum length is 3.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Get Equal Substrings Within Budget
```go
```
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to character in t, so the maximum length is 1.
```

**Example 3:**

```
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You cannot make any change, so the maximum length is 1.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string, t string, maxCost int) int {
start, end := 0, 0
ans, cur, diff := 0, 0, 0
for ; end < len(s); end++ {
if s[end] > t[end] {
diff = int(s[end] - t[end])
} else {
diff = int(t[end] - s[end])
}
cur += diff
for cur > maxCost && start <= end {
if s[start] > t[start] {
diff = int(s[start] - t[start])
} else {
diff = int(t[start] - s[start])
}
cur -= diff
start++
}
ans = max(ans, end-start+1)
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
ss, tt string
maxCost int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abcd", "bcdf", 3, 3},
{"TestCase2", "abcd", "cdef", 3, 1},
{"TestCase3", "abcd", "acde", 0, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.ss, c.tt, c.maxCost)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.ss, c.tt, c.maxCost)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit c2ab7c3

Please sign in to comment.