Gourab_Chatterjee's blog

By Gourab_Chatterjee, 3 weeks ago, In English

Hi all, This is in context to https://leetcode.com/problems/total-characters-in-string-after-transformations-ii/description/ I watched Errichto's intro to matrix exponentiation video wherein he provides a general formula for converting transitions to matrix coefficients , [ new_dp[j]+=dp[i] * m[i][j] ], post which I implemented the following :

for(int i=1;i<=t;i++)
{
    vector<int>new_dp(26);
    for(int j=0;j<26;j++)
    {
        for(int k=1;k<=nums[j];k++)
            new_dp[(j+k)%26]+=dp[j];
    }

    dp=new_dp;
}

which leads to single.grid[j][(j+k)%26] += 1;

My question is post calculating result = matrix^n how do we determine whether we need to multiply the [base case matrix] * result or result * [base case matrix] .... [In here the base case matrix is just the frequency count of each character in the original string].

In this case if I am multiplying freq * result it's giving me the correct answer but if I were to initialize single.grid[(j+k)%26][j] += 1; then result * freq gives me the correct values.

Please help.

Full text and comments »