-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (59 loc) · 1.76 KB
/
main.go
File metadata and controls
65 lines (59 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Source: https://leetcode.com/problems/shifting-letters
// Title: Shifting Letters
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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: 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:**
//
// ```
// Input: s = "aaa", shifts = [1,2,3]
// Output: "gfd"
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `s` consists of lowercase English letters.
// - `shifts.length == s.length`
// - `0 <= shifts[i] <= 10^9`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func shiftingLetters(s string, shifts []int) string {
n := len(s)
bytes := []byte(s)
move := 0
for i := n - 1; i >= 0; i-- {
move += shifts[i]
bytes[i] = byte(_mod(int(bytes[i]-'a')+move, 26)) + 'a'
}
return string(bytes)
}
func _mod(a, b int) int {
a %= b
if a < 0 {
a += b
}
return a
}