Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions solution/2300-2399/2381.Shifting Letters II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ func shiftingLetters(s string, shifts [][]int) string {
}
```

```ts
function shiftingLetters(s: string, shifts: number[][]): string {
const n: number = s.length;
const d: number[] = new Array(n + 1).fill(0);

for (let [i, j, v] of shifts) {
if (v === 0) {
v--;
}
d[i] += v;
d[j + 1] -= v;
}

for (let i = 1; i <= n; ++i) {
d[i] += d[i - 1];
}

let ans: string = '';
for (let i = 0; i < n; ++i) {
const j = (s.charCodeAt(i) - 'a'.charCodeAt(0) + (d[i] % 26) + 26) % 26;
ans += String.fromCharCode('a'.charCodeAt(0) + j);
}

return ans;
}
```

<!-- tabs:end -->

<!-- end -->
27 changes: 27 additions & 0 deletions solution/2300-2399/2381.Shifting Letters II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ func shiftingLetters(s string, shifts [][]int) string {
}
```

```ts
function shiftingLetters(s: string, shifts: number[][]): string {
const n: number = s.length;
const d: number[] = new Array(n + 1).fill(0);

for (let [i, j, v] of shifts) {
if (v === 0) {
v--;
}
d[i] += v;
d[j + 1] -= v;
}

for (let i = 1; i <= n; ++i) {
d[i] += d[i - 1];
}

let ans: string = '';
for (let i = 0; i < n; ++i) {
const j = (s.charCodeAt(i) - 'a'.charCodeAt(0) + (d[i] % 26) + 26) % 26;
ans += String.fromCharCode('a'.charCodeAt(0) + j);
}

return ans;
}
```

<!-- tabs:end -->

<!-- end -->
24 changes: 24 additions & 0 deletions solution/2300-2399/2381.Shifting Letters II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function shiftingLetters(s: string, shifts: number[][]): string {
const n: number = s.length;
const d: number[] = new Array(n + 1).fill(0);

for (let [i, j, v] of shifts) {
if (v === 0) {
v--;
}
d[i] += v;
d[j + 1] -= v;
}

for (let i = 1; i <= n; ++i) {
d[i] += d[i - 1];
}

let ans: string = '';
for (let i = 0; i < n; ++i) {
const j = (s.charCodeAt(i) - 'a'.charCodeAt(0) + (d[i] % 26) + 26) % 26;
ans += String.fromCharCode('a'.charCodeAt(0) + j);
}

return ans;
}