Java - simulation - single array and answer variable!
There are many other posts involving a copy array to update each iteration - while valid solution, it is not really needed and is hard to understand.\n\n# Intui
There are many other posts involving a copy array to update each iteration - while valid solution, it is not really needed and is hard to understand.\n\n# Intuition \n- The intuition is that the constraints are small enough to run a linear time algorithm. O(cN) where c is a constant is still linear and so counting frequency of each of the 26 characters will lead to c=26 and hence a passing solution \n- Start by initializing the frequency of each character and answer = length of input string \n- In each of the `t` iterations:\n    - For every Z in this iteration, the length increases by 1 since `z -> ab`. So, `answer = answer + f[25]`;\n    - `f[j] = f[j-1]` - shift the frequency - for all j between 25 and 1\n    - `f[1] = frequency of a before shift + frequency of z before shift`\n    - `f[0] = frequency of z before shift`\n\n# Code\n```java []\nclass Solution {\n    public int lengthAfterTransformations(String s, int t) {\n        // Initialize answer and frequency with input S. \n        int[] frequency = new int[26];\n        for(int i=0;i<s.length();i++) {\n            frequency[s.charAt(i) - \'a\']++;\n        }\n        int answer = s.length();\n\n        // Simulate each iteration \n        for(int i=0;i<t;i++) {\n            // Increment answer by count of Z\n            answer = (answer + frequency[25]) % 1000000007;\n            int x = (int)frequency[25];\n\n            // Shift the frequency since each char -> char + 1\n            for(int j=25;j>=1;j--) {\n                frequency[j] = frequency[j-1];\n            }\n            // frequency[b] = old a (which is already shifted) + old z;\n            frequency[1] = (frequency[1] + x)% 1000000007; \n\n            // frequency[a] =  + old z;\n            frequency[0] = (x)% 1000000007;\n        }\n        return answer;\n    }\n}\n```
['Array', 'Hash Table', 'Simulation', 'Java']