String Evolution: The Transformation Challenge ✅🔥 || EASY CPP HASHING SOLUTION
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThe approach counts character frequencies and updates them for t transfor
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe approach counts character frequencies and updates them for t transformations to calculate the final length without creating a large string, making it more efficient than brute force.\n\n# Complexity\n- Time complexity:\n- 0(T*26)\n\n- Space complexity:\nO(26)\n\n# Code\n```c++ []\nclass Solution {\npublic:\n    int lengthAfterTransformations(string s, int t) {\n        const int MOD = 1e9 + 7;\n        vector<long long> cnt(26, 0);\n\n        for (char c : s) {\n            cnt[c - \'a\']++;\n        }\n\n        for (int j = 0; j < t; j++) {\n            vector<long long> tmp(26, 0);\n            for (int i = 0; i < 26; i++) {\n                if (i == 25) {\n                    tmp[0] = (tmp[0] + cnt[i]) % MOD;\n                    tmp[1] = (tmp[1] + cnt[i]) % MOD;\n                } else {\n                    tmp[i + 1] = (tmp[i + 1] + cnt[i]) % MOD;\n                }\n            }\n            cnt = tmp;\n        }\n\n        long long len = 0;\n        for (long long c : cnt) {\n            len = (len + c) % MOD;\n        }\n\n        return len;\n    }\n};\n```\n```javascript []\nclass Solution {\n    lengthAfterTransformations(s, t) {\n        const MOD = 1e9 + 7;\n        const cnt = new Array(26).fill(0);\n\n        for (const c of s) {\n            cnt[c.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n        }\n\n        for (let j = 0; j < t; j++) {\n            const tmp = new Array(26).fill(0);\n            for (let i = 0; i < 26; i++) {\n                if (i === 25) {\n                    tmp[0] = (tmp[0] + cnt[i]) % MOD;\n                    tmp[1] = (tmp[1] + cnt[i]) % MOD;\n                } else {\n                    tmp[i + 1] = (tmp[i + 1] + cnt[i]) % MOD;\n                }\n            }\n            cnt.splice(0, 26, ...tmp); // Update cnt with tmp values\n        }\n\n        return cnt.reduce((len, c) => (len + c) % MOD, 0);\n    }\n}\n\n\n```\n```python []\nclass Solution:\n    def lengthAfterTransformations(self, s: str, t: int) -> int:\n        MOD = 10**9 + 7\n        cnt = [0] * 26\n\n        for c in s:\n            cnt[ord(c) - ord(\'a\')] += 1\n\n        for _ in range(t):\n            tmp = [0] * 26\n            for i in range(26):\n                if i == 25:\n                    tmp[0] = (tmp[0] + cnt[i]) % MOD\n                    tmp[1] = (tmp[1] + cnt[i]) % MOD\n                else:\n                    tmp[i + 1] = (tmp[i + 1] + cnt[i]) % MOD\n            cnt = tmp\n\n        return sum(cnt) % MOD\n\n```\n```Java []\nclass Solution {\n    public int lengthAfterTransformations(String s, int t) {\n        final int MOD = 1000000007;\n        long[] cnt = new long[26];\n\n        for (char c : s.toCharArray()) {\n            cnt[c - \'a\']++;\n        }\n\n        for (int j = 0; j < t; j++) {\n            long[] tmp = new long[26];\n            for (int i = 0; i < 26; i++) {\n                if (i == 25) {\n                    tmp[0] = (tmp[0] + cnt[i]) % MOD;\n                    tmp[1] = (tmp[1] + cnt[i]) % MOD;\n                } else {\n                    tmp[i + 1] = (tmp[i + 1] + cnt[i]) % MOD;\n                }\n            }\n            cnt = tmp;\n        }\n\n        long len = 0;\n        for (long c : cnt) {\n            len = (len + c) % MOD;\n        }\n\n        return (int) len;\n    }\n}\n\n```\n\n\n![upvote.jpg](https://assets.leetcode.com/users/images/1d9a2db2-4f04-4fb6-aa39-b7cd92218fb3_1730012289.1290357.jpeg)
['Hash Table', 'Hash Function', 'C++', 'Java', 'Python3', 'JavaScript']