Skip to content

Add solution and test-cases for problem 848 #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions leetcode/801-900/0848.Shifting-Letters/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [848.Shifting Letters][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 a string `s` of lowercase English letters and an integer array `shifts` of the same length.

Call the `shift()` of a letter, the next letter in the alphabet, (wrapping around so that `'z'` becomes `'a'`).

- For example, `shift('a') = 'b'`, `shift('t') = 'u'`, and `shift('z') = 'a'`.

Now for each `shifts[i] = x`, we want to shift the first `i + 1` letters of `s`, x times.

Return the final string after all such shifts to s are applied.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: We start with "abc".
After shifting the first 1 letters of s by 3, we have "dbc".
After shifting the first 2 letters of s by 5, we have "igc".
After shifting the first 3 letters of s by 9, we have "rpl", the answer.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Shifting Letters
```go
```

Input: s = "aaa", shifts = [1,2,3]
Output: "gfd"
```

## 结语

Expand Down
11 changes: 9 additions & 2 deletions leetcode/801-900/0848.Shifting-Letters/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string, shifts []int) string {
ans := []byte(s)
sum := 0
for i := len(s) - 1; i >= 0; i-- {
sum += shifts[i]
now := (int(ans[i]-'a') + sum) % 26
ans[i] = byte(now) + 'a'
}
return string(ans)
}
16 changes: 8 additions & 8 deletions leetcode/801-900/0848.Shifting-Letters/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
shifts []int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abc", []int{3, 5, 9}, "rpl"},
{"TestCase2", "aaa", []int{1, 2, 3}, "gfd"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.shifts)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

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