diff --git a/leetcode/801-900/0848.Shifting-Letters/README.md b/leetcode/801-900/0848.Shifting-Letters/README.md index 8f33b5713..8a9a71ed6 100644 --- a/leetcode/801-900/0848.Shifting-Letters/README.md +++ b/leetcode/801-900/0848.Shifting-Letters/README.md @@ -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" +``` ## 结语 diff --git a/leetcode/801-900/0848.Shifting-Letters/Solution.go b/leetcode/801-900/0848.Shifting-Letters/Solution.go index d115ccf5e..299e5b84f 100644 --- a/leetcode/801-900/0848.Shifting-Letters/Solution.go +++ b/leetcode/801-900/0848.Shifting-Letters/Solution.go @@ -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) } diff --git a/leetcode/801-900/0848.Shifting-Letters/Solution_test.go b/leetcode/801-900/0848.Shifting-Letters/Solution_test.go index 14ff50eb4..539a3bb04 100644 --- a/leetcode/801-900/0848.Shifting-Letters/Solution_test.go +++ b/leetcode/801-900/0848.Shifting-Letters/Solution_test.go @@ -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) @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }