Clean Code || Easy to Understand || Freq Array
Approach\nIts obvious that we cant make the whole string after tranformations as the string length is 1e5 and t is also 1e5 .\n\nSo the only option remains is t
# Approach\nIts obvious that we cant make the whole string after tranformations as the string length is 1e5 and t is also 1e5 .\n\nSo the only option remains is to add frequencies in the freq array .\n\n**Important Note -> Start adding frequencies from the last character \'z\' because a is converted to b , b to c , ... y to z .**\n\nIf we start from a then additional frequencies will also be add up. \neg. we have abc , if we start from a , then freq  of b will be its curr freq + freq[a] , now this will be converted to c , but we dont want freq[a] to be added in c , **thats why start from z** .\n\n# Complexity\n- Time complexity: O(t+n)\nO(n) for counting the frequency of characters in the string initially.\nO(t*26) for the transformation process\n\n- Space complexity: O(1)\nThe space used for the freq array is constant (26 elements) regardless of the input size.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int lengthAfterTransformations(string s, int t) {\n        vector<int> freq(26, 0);\n        int MOD = 1e9 + 7;\n\n        for (char ch : s) {\n            freq[ch - \'a\'] = (freq[ch - \'a\'] + 1) % MOD;\n        }\n\n        while (t--) {\n            int zCount = freq[25];\n            freq[25] = 0;\n\n            for (int i = 24; i >= 0; i--) {\n                freq[i + 1] = freq[i];\n                freq[i] = 0;\n            }\n\n            // convert z into a and b \n            if (zCount) {\n                freq[0] = (freq[0] + zCount) % MOD;\n                freq[1] = (freq[1] + zCount) % MOD;\n            }\n        }\n\n        int totalLength = 0;\n        for (int i = 0; i < 26; i++) {\n            totalLength = (totalLength + freq[i]) % MOD;\n        }\n\n        return totalLength;\n    }\n};\n\n```\n```java []\nclass Solution {\n    public int lengthAfterTransformations(String s, int t) {\n        int[] freq = new int[26];\n        int MOD = 1_000_000_007;\n\n        for (char ch : s.toCharArray()) {\n            freq[ch - \'a\'] = (freq[ch - \'a\'] + 1) % MOD;\n        }\n\n        while (t-- > 0) {\n            int zCount = freq[25];\n            freq[25] = 0;\n\n            for (int i = 24; i >= 0; i--) {\n                freq[i + 1] = freq[i];\n                freq[i] = 0;\n            }\n\n            // convert \'z\' into \'a\' and \'b\'\n            if (zCount > 0) {\n                freq[0] = (freq[0] + zCount) % MOD;\n                freq[1] = (freq[1] + zCount) % MOD;\n            }\n        }\n\n        int totalLength = 0;\n        for (int count : freq) {\n            totalLength = (totalLength + count) % MOD;\n        }\n\n        return totalLength;\n    }\n}\n\n\n```\n\n```javascript []\nclass Solution {\n    lengthAfterTransformations(s, t) {\n        const MOD = 1e9 + 7;\n        let freq = Array(26).fill(0);\n\n        for (let ch of s) {\n            freq[ch.charCodeAt(0) - \'a\'.charCodeAt(0)] = (freq[ch.charCodeAt(0) - \'a\'.charCodeAt(0)] + 1) % MOD;\n        }\n\n        while (t-- > 0) {\n            let zCount = freq[25];\n            freq[25] = 0;\n\n            for (let i = 24; i >= 0; i--) {\n                freq[i + 1] = freq[i];\n                freq[i] = 0;\n            }\n\n            // Convert \'z\' into \'a\' and \'b\'\n            if (zCount > 0) {\n                freq[0] = (freq[0] + zCount) % MOD;\n                freq[1] = (freq[1] + zCount) % MOD;\n            }\n        }\n\n        let totalLength = 0;\n        for (let count of freq) {\n            totalLength = (totalLength + count) % MOD;\n        }\n\n        return totalLength;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def lengthAfterTransformations(self, s: str, t: int) -> int:\n        MOD = int(1e9 + 7)\n        freq = [0] * 26\n\n        for ch in s:\n            freq[ord(ch) - ord(\'a\')] = (freq[ord(ch) - ord(\'a\')] + 1) % MOD\n\n        for _ in range(t):\n            zCount = freq[25]\n            freq[25] = 0\n\n            for i in range(24, -1, -1):\n                freq[i + 1] = freq[i]\n                freq[i] = 0\n\n            # Convert \'z\' into \'a\' and \'b\'\n            if zCount > 0:\n                freq[0] = (freq[0] + zCount) % MOD\n                freq[1] = (freq[1] + zCount) % MOD\n\n        totalLength = sum(freq) % MOD\n        return totalLength\n\n```\n`Upvote if this helped`\n
['Array', 'Python', 'C++', 'Java', 'JavaScript']