Skip to content

Commit ea8e990

Browse files
add 848
1 parent 09c7200 commit ea8e990

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ _If you like this project, please leave me a star._ ★
502502
|856|[Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_856.java) | |Medium| Stack, String
503503
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | |Easy|
504504
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_849.java) | |Easy|
505+
|848|[Shifting Letters](https://leetcode.com/problems/shifting-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_848.java) | |Medium| Array, String
505506
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | |Easy|
506507
|841|[Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_841.java) | |Easy|DFS, Graph
507508
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _848 {
4+
public static class Solution1 {
5+
public String shiftingLetters(String s, int[] shifts) {
6+
long[] preSums = new long[shifts.length];
7+
for (int i = shifts.length - 1; i >= 0; i--) {
8+
if (i < shifts.length - 1) {
9+
preSums[i] = preSums[i + 1] + shifts[i];
10+
} else {
11+
preSums[i] = shifts[i];
12+
}
13+
preSums[i] %= 26;
14+
}
15+
StringBuilder sb = new StringBuilder();
16+
for (int i = 0; i < s.length(); i++) {
17+
int newChar = s.charAt(i) + (int) preSums[i] % 26;
18+
if (newChar > 122) {
19+
sb.append((char) (newChar - 122 + 96));
20+
} else {
21+
sb.append((char) (newChar));
22+
}
23+
}
24+
return sb.toString();
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._848;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _848Test {
10+
private static _848.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _848.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("rpl", solution1.shiftingLetters("abc", new int[]{3, 5, 9}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("gfd", solution1.shiftingLetters("aaa", new int[]{1, 2, 3}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("rul", solution1.shiftingLetters("ruu", new int[]{26, 9, 17}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("wqqwlcjnkphhsyvrkdod", solution1.shiftingLetters("mkgfzkkuxownxvfvxasy", new int[]{505870226, 437526072, 266740649, 224336793, 532917782, 311122363, 567754492, 595798950, 81520022, 684110326, 137742843, 275267355, 856903962, 148291585, 919054234, 467541837, 622939912, 116899933, 983296461, 536563513}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)