Skip to content

Commit 95ed7dc

Browse files
committed
Add solution and test-cases for problem 848
1 parent 8ae5d85 commit 95ed7dc

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

leetcode/801-900/0848.Shifting-Letters/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [848.Shifting Letters][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 a string `s` of lowercase English letters and an integer array `shifts` of the same length.
5+
6+
Call the `shift()` of a letter, the next letter in the alphabet, (wrapping around so that `'z'` becomes `'a'`).
7+
8+
- For example, `shift('a') = 'b'`, `shift('t') = 'u'`, and `shift('z') = 'a'`.
9+
10+
Now for each `shifts[i] = x`, we want to shift the first `i + 1` letters of `s`, x times.
11+
12+
Return the final string after all such shifts to s are applied.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: s = "abc", shifts = [3,5,9]
18+
Output: "rpl"
19+
Explanation: We start with "abc".
20+
After shifting the first 1 letters of s by 3, we have "dbc".
21+
After shifting the first 2 letters of s by 5, we have "igc".
22+
After shifting the first 3 letters of s by 9, we have "rpl", the answer.
1323
```
1424

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

20-
### 思路1
21-
> ...
22-
Shifting Letters
23-
```go
2427
```
25-
28+
Input: s = "aaa", shifts = [1,2,3]
29+
Output: "gfd"
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, shifts []int) string {
4+
ans := []byte(s)
5+
sum := 0
6+
for i := len(s) - 1; i >= 0; i-- {
7+
sum += shifts[i]
8+
now := (int(ans[i]-'a') + sum) % 26
9+
ans[i] = byte(now) + 'a'
10+
}
11+
return string(ans)
512
}

leetcode/801-900/0848.Shifting-Letters/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
shifts []int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abc", []int{3, 5, 9}, "rpl"},
18+
{"TestCase2", "aaa", []int{1, 2, 3}, "gfd"},
1919
}
2020

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

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)