Skip to content

Commit 3a32817

Browse files
cblhthinkasany
andauthored
feat: add ts solution to lc problem: No.2381 (#2732)
* feat: update ts solutions to lc problem: No.2381 * Update solution/2300-2399/2381.Shifting Letters II/README.md * Update solution/2300-2399/2381.Shifting Letters II/Solution.ts * Update solution/2300-2399/2381.Shifting Letters II/README_EN.md --------- Co-authored-by: thinkasany <480968828@qq.com>
1 parent d9f9cda commit 3a32817

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

solution/2300-2399/2381.Shifting Letters II/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ func shiftingLetters(s string, shifts [][]int) string {
147147
}
148148
```
149149

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+
150177
<!-- tabs:end -->
151178

152179
<!-- end -->

solution/2300-2399/2381.Shifting Letters II/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ func shiftingLetters(s string, shifts [][]int) string {
139139
}
140140
```
141141

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+
142169
<!-- tabs:end -->
143170

144171
<!-- end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)