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:
'z', replace it with the string "ab".'a' is replaced with 'b', 'b' is replaced with 'c', and so on.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:
'a' becomes 'b''b' becomes 'c''c' becomes 'd''y' becomes 'z''y' becomes 'z'"bcdzz"'b' becomes 'c''c' becomes 'd''d' becomes 'e''z' becomes "ab"'z' becomes "ab""cdeabab""cdeabab", which has 7 characters.Example 2:
Input: s = “azbk”, t = 1
Output: 5
Explanation:
'a' becomes 'b''z' becomes "ab"'b' becomes 'c''k' becomes 'l'"babcl""babcl", which has 5 characters.Constraints:
1 <= s.length <= 105s consists only of lowercase English letters.1 <= t <= 105import 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
}
}