LeetCode in Kotlin

3335. Total Characters in String After Transformations I

Medium

You are given a string s and an integer t, representing the number of transformations to perform. In one transformation, every character in s is replaced according to the following rules:

Return the length of the resulting string after exactly t transformations.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: s = “abcyy”, t = 2

Output: 7

Explanation:

Example 2:

Input: s = “azbk”, t = 1

Output: 5

Explanation:

Constraints:

Solution

import java.util.LinkedList

class Solution {
    fun lengthAfterTransformations(s: String, t: Int): Int {
        val count = IntArray(26)
        for (c in s.toCharArray()) {
            count[c.code - 'a'.code]++
        }
        val list = LinkedList<Int?>()
        for (c in count) {
            list.add(c)
        }
        var delta = s.length % 1000000007
        for (i in 0 until t) {
            val zCount = list.removeLast()!! % 1000000007
            val aCount = list.pollFirst()!! % 1000000007
            list.offerFirst((aCount + zCount) % 1000000007)
            list.offerFirst(zCount)
            delta = delta % 1000000007
            delta = (delta + zCount) % 1000000007
        }
        return delta
    }
}