File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
solution/2300-2399/2381.Shifting Letters II Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,33 @@ func shiftingLetters(s string, shifts [][]int) string {
147
147
}
148
148
```
149
149
150
+ ``` ts
151
+ function shiftingLetters(s : string , shifts : number [][]): string {
152
+ const n: number = s .length ;
153
+ const d: number [] = new Array (n + 1 ).fill (0 );
154
+
155
+ for (let [i, j, v] of shifts ) {
156
+ if (v === 0 ) {
157
+ v -- ;
158
+ }
159
+ d [i ] += v ;
160
+ d [j + 1 ] -= v ;
161
+ }
162
+
163
+ for (let i = 1 ; i <= n ; ++ i ) {
164
+ d [i ] += d [i - 1 ];
165
+ }
166
+
167
+ let ans: string = ' ' ;
168
+ for (let i = 0 ; i < n ; ++ i ) {
169
+ const j = (s .charCodeAt (i ) - ' a' .charCodeAt (0 ) + (d [i ] % 26 ) + 26 ) % 26 ;
170
+ ans += String .fromCharCode (' a' .charCodeAt (0 ) + j );
171
+ }
172
+
173
+ return ans ;
174
+ }
175
+ ```
176
+
150
177
<!-- tabs: end -->
151
178
152
179
<!-- end -->
Original file line number Diff line number Diff line change @@ -139,6 +139,33 @@ func shiftingLetters(s string, shifts [][]int) string {
139
139
}
140
140
```
141
141
142
+ ``` ts
143
+ function shiftingLetters(s : string , shifts : number [][]): string {
144
+ const n: number = s .length ;
145
+ const d: number [] = new Array (n + 1 ).fill (0 );
146
+
147
+ for (let [i, j, v] of shifts ) {
148
+ if (v === 0 ) {
149
+ v -- ;
150
+ }
151
+ d [i ] += v ;
152
+ d [j + 1 ] -= v ;
153
+ }
154
+
155
+ for (let i = 1 ; i <= n ; ++ i ) {
156
+ d [i ] += d [i - 1 ];
157
+ }
158
+
159
+ let ans: string = ' ' ;
160
+ for (let i = 0 ; i < n ; ++ i ) {
161
+ const j = (s .charCodeAt (i ) - ' a' .charCodeAt (0 ) + (d [i ] % 26 ) + 26 ) % 26 ;
162
+ ans += String .fromCharCode (' a' .charCodeAt (0 ) + j );
163
+ }
164
+
165
+ return ans ;
166
+ }
167
+ ```
168
+
142
169
<!-- tabs: end -->
143
170
144
171
<!-- end -->
Original file line number Diff line number Diff line change
1
+ function shiftingLetters ( s : string , shifts : number [ ] [ ] ) : string {
2
+ const n : number = s . length ;
3
+ const d : number [ ] = new Array ( n + 1 ) . fill ( 0 ) ;
4
+
5
+ for ( let [ i , j , v ] of shifts ) {
6
+ if ( v === 0 ) {
7
+ v -- ;
8
+ }
9
+ d [ i ] += v ;
10
+ d [ j + 1 ] -= v ;
11
+ }
12
+
13
+ for ( let i = 1 ; i <= n ; ++ i ) {
14
+ d [ i ] += d [ i - 1 ] ;
15
+ }
16
+
17
+ let ans : string = '' ;
18
+ for ( let i = 0 ; i < n ; ++ i ) {
19
+ const j = ( s . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) + ( d [ i ] % 26 ) + 26 ) % 26 ;
20
+ ans += String . fromCharCode ( 'a' . charCodeAt ( 0 ) + j ) ;
21
+ }
22
+
23
+ return ans ;
24
+ }
You can’t perform that action at this time.
0 commit comments