g2301_2400.s2381_shifting_letters_ii.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2381_shifting_letters_ii
// #Medium #Array #String #Prefix_Sum #2023_07_02_Time_669_ms_(100.00%)_Space_70_MB_(100.00%)
class Solution {
fun shiftingLetters(s: String, shifts: Array): String {
val diff = IntArray(s.length + 1)
var l: Int
var r: Int
for (shift in shifts) {
l = shift[0]
r = shift[1] + 1
diff[l] += 26
diff[r] += 26
if (shift[2] == 0) {
diff[l]--
diff[r]++
} else {
diff[l]++
diff[r]--
}
diff[l] %= 26
diff[r] %= 26
}
val sb = StringBuilder()
var current = 0
var `val`: Int
for (i in 0 until s.length) {
current += diff[i]
`val` = s[i].code - 'a'.code
`val` += current
`val` %= 26
sb.append(('a'.code + `val`).toChar())
}
return sb.toString()
}
}