Approach

This is a really hardr problem, originally I was thinking that for some string like a.. and b.., we must find somethiong to replace the first character, but I did not notice that the problme describe that the changing substring cannot be overlap (it can only be seperate or the same range). I originally think that ace as source and bcd as target, we might have a cost like ce to ed and aed to bcd, then we can combine both to transit to the target.

If the substr cannot be overlap, we will try to find all changing pairs that can change the currnet character, and we need to check if we can actaully do so based on the current string. If we can change it, we change it and the problem become a subproblem, thus we can use DP to track this.

For the cost part, we apply the Floyd-Warshall algorithm to get the ASAP min cost. Then the second part is doing DP to combine them, but my original solution was getting TLE since I am using the length of the source string for inner loop, and this can be with order 1000, also I need to copy the substr and chekc if the string is inside the hashmap. A faster approach is to check the substr inside the hashmap, and we can have a slighly faster comparison, also we can leverage compare function, which will not do string copy.

Also, original I add continue for the case that the i-th character is the same, this is not quite correct since replacing the following subproblem could have a lower cost!

Code

class Solution {
public:
    long long minimumCost(string source, string target, vector<string>& original, vector<string>& changed, vector<int>& cost) {
        int n = source.size();
        int m = original.size();

        int totalCnt = 0;
        unordered_map<string, int> mp; // string, nodeIdx
        for(int i = 0 ; i < m ; ++i) {
            mp[original[i]] = totalCnt++;
            mp[changed[i]] = totalCnt++;
        }
        vector<vector<long long>> dist(totalCnt, vector<long long>(totalCnt, LLONG_MAX));
        for(int i = 0 ; i < m ; ++i) {
            int u = mp[original[i]];
            int v = mp[changed[i]];
            dist[u][v] = min(dist[u][v], (long long)cost[i]);
        }
        for(int i = 0 ; i < totalCnt ; ++i) dist[i][i] = 0;

        // APSP
        for(int k = 0 ; k < totalCnt ; ++k) {
            for(int i = 0 ; i < totalCnt ; ++i) {
                for(int j = 0 ; j < totalCnt ; ++j) {
                    if(dist[i][k] == LLONG_MAX || dist[k][j] == LLONG_MAX) continue;
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }

        vector<long long> minCost(n, LLONG_MAX);
        minCost.push_back(0);
        for(int i = n-1 ; i >= 0 ; --i) {
            if(source[i] == target[i]) {
                minCost[i] = minCost[i+1];
            }
            // for(int len = 1 ; i + len <= n; ++len) {
            //     string s = source.substr(i, len);
            //     if(!mp.count(s)) continue;
            //     string t = target.substr(i, len);
            //     if(!mp.count(t)) continue;
            //     if(dist[mp[s]][mp[t]] == LLONG_MAX || minCost[i+len] == LLONG_MAX) continue;
            //     minCost[i] = min(minCost[i], dist[mp[s]][mp[t]] + minCost[i+len]);
            // }
            for (const auto& kvS : mp) {
                const string& s = kvS.first;
                int u = kvS.second;
                int len = (int)s.size();

                if (i + len > n) continue;
                if (source.compare(i, len, s) != 0) continue;
                if (minCost[i + len] == LLONG_MAX) continue;

                // find target substring with SAME len by iterating mp again
                for (const auto& kvT : mp) {
                    const string& t = kvT.first;
                    int v = kvT.second;

                    if ((int)t.size() != len) continue;
                    if (target.compare(i, len, t) != 0) continue;

                    if (dist[u][v] == LLONG_MAX) continue;
                    minCost[i] = min(minCost[i], dist[u][v] + minCost[i + len]);
                }
            }
        }

        return minCost[0] == LLONG_MAX ? -1 : minCost[0];
    }
};