LeetCode 3337: Total Characters in String After Transformations II

Problem Description

Explanation:

  • The problem can be solved by simulating the transformations on the string s for t iterations.
  • For each character in the string, we calculate its transformed version based on the nums array.
  • We update the string s with the transformed characters.
  • Repeat this process for t iterations and return the length of the final transformed string modulo 10^9 + 7.

Time Complexity: O(t * n) where n is the length of the input string s. Space Complexity: O(n) for storing the transformed string.

:

Solutions

class Solution {
    public int totalCharacters(String s, int t, int[] nums) {
        int mod = 1000000007;
        for (int i = 0; i < t; i++) {
            StringBuilder sb = new StringBuilder();
            for (char c : s.toCharArray()) {
                int index = c - 'a';
                for (int j = 0; j < nums[index]; j++) {
                    sb.append((char)('a' + (index + j) % 26));
                }
            }
            s = sb.toString();
        }
        return s.length() % mod;
    }
}

Loading editor...