Skip to content

Commit 31fd53a

Browse files
add 848
1 parent 8aed66a commit 31fd53a

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.array.maximumTripletValue
5-
import com.github.contest.array.maximumTripletValue
4+
import com.github.contest.strings.shiftingLetters
65

76
import java.util.TreeMap
87

@@ -13,12 +12,7 @@ import java.util.TreeMap
1312

1413
fun main() {
1514

16-
maximumTripletValue(
17-
intArrayOf(
18-
15, 3, 3, 18, 19, 13, 7, 5, 18, 1, 8, 5
19-
)
20-
).also { println(it) }
21-
15+
shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) }
2216
}
2317

2418
fun testing() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
package com.github.contest.strings
22

3+
/**
4+
* 848. Shifting Letters
5+
*/
6+
7+
fun shiftingLetters(str: String, shifts: IntArray): String {
8+
val shifting = IntArray(shifts.size)
9+
var sum = shifts.sum()
10+
var res = ""
11+
12+
for (i in shifting.indices) {
13+
shifting[i] = sum
14+
sum -= shifts[i]
15+
}
16+
17+
for (i in str.indices) {
18+
val newCharValue = shiftLetter(str[i], shifting[i])
19+
res += newCharValue
20+
21+
}
22+
23+
return res
24+
}
25+
26+
private fun shiftLetter(char: Char, shift: Int): Char = when {
27+
char in 'a'..'z' -> {
28+
val base = 'a'.code
29+
val offset = char.code - base
30+
val shifted = (offset + shift) % 26
31+
(base + shifted).toChar()
32+
}
33+
else -> char
34+
}
35+

0 commit comments

Comments
 (0)