All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2381_shifting_letters_ii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy