Approach

This is a dp-like problem, and I was original thinking that for a - b - a - c, we try to match c - a - b as possible. when we match c, we find that it is not possible, when matching a, we would also match the first occurence, since matching a latter one would not be better. Also, we can try to not match the current character, and try to do insertion for this a. This can be recursively done by solvinf problem from the end.

Let the comparison index be idx1 and idx2, if we want to solve bet match of str1(idx1), str2(idx2), we can easily form the solution by its subproblem by checking subsolution str1(idx1+1/0), str2(idx2+1/0), and notice that we can proceed both pointer only when the current character is the same.

Code-1 is my original solution, which is having MLE since I am hashing the string in the memo, which would be extramely large. A better approach in Code-2 is to store the best cost and the mode (picking from str1 or str2 or both), and we can form the string in the end of the dp process.

Also another part is that I used a hashmap originally to store the answer, and in this case we can use the vector instead, this can also increase some runtime.

Code-1: Dp hashing string (MLE)

class Solution {
public:
    string shortestCommonSupersequence(string str1, string str2) {
        if(str2.size() > str1.size()) swap(str1, str2);
        int m = str1.size();
        int n = str2.size();

        pair<int,int> sol = minCost(str1, str2, 0, 0);

        int idx1 = 0;
        int idx2 = 0;
        string ans = "";
        while(idx1 < m || idx2 < n) {
            int key = idx1 * 1001 + idx2;
            assert(memo.count(key));
            int mode = memo[key].second;
            if(mode == 0) {
                ans.push_back(str2[idx2]);
                ++idx2;
            } else if(mode == 1) {
                ans.push_back(str1[idx1]);
                ++idx1;
            } else if(mode == 3) {
                ans.push_back(str1[idx1]);
                ++idx2;
                ++idx1;
            } else if(mode == 4) {
                ans += str1.substr(idx1, str1.size()-idx1);
                break;
            } else {
                ans += str2.substr(idx2, str2.size()-idx2);
                break;
            }
        }
        return ans;
    }

    pair<int, int> minCost(const string& str1, const string& str2, int idx1, int idx2) {
        int key = idx1 * 1001 + idx2;

        if(idx2 == str2.size()) {
            memo[key] = {(int)str1.size() - idx1, 4};
            return memo[key];
        }
        if(idx1 == str1.size()) {
            memo[key] = {(int)str2.size() - idx2, 5};
            return memo[key];
        }

        if(memo.count(key)) return memo[key];
        pair<int,int> ans;
        
        if(str1[idx1] == str2[idx2]) {
            ans = {minCost(str1, str2, idx1+1, idx2+1).first+1, 3};
        } else {
            int sz1 = minCost(str1, str2, idx1, idx2+1).first;
            int sz2 = minCost(str1, str2, idx1+1, idx2).first;
            if(sz1 < sz2) ans = {sz1+1, 0};
            else ans = {sz2+1, 1};
        }
        memo[key] = ans;
        return ans;
    }

    unordered_map<int, pair<int, int>> memo; // key -> <cost, direction>
};

Code-2: Dp hashing cost and picking strategy + re-calculate string

class Solution {
public:
    string shortestCommonSupersequence(string str1, string str2) {
        if(str2.size() > str1.size()) swap(str1, str2);
        int m = str1.size();
        int n = str2.size();

        memo.assign(m+1, vector<pair<int,int>>(n+1, {INT_MAX, 0}));
        pair<int,int> sol = minCost(str1, str2, 0, 0);

        int idx1 = 0;
        int idx2 = 0;
        string ans = "";
        while(idx1 < m || idx2 < n) {
            assert(memo[idx1][idx2].first != INT_MAX);
            int mode = memo[idx1][idx2].second;
            if(mode == 0) {
                ans.push_back(str2[idx2]);
                ++idx2;
            } else if(mode == 1) {
                ans.push_back(str1[idx1]);
                ++idx1;
            } else if(mode == 3) {
                ans.push_back(str1[idx1]);
                ++idx2;
                ++idx1;
            } else if(mode == 4) {
                ans += str1.substr(idx1, str1.size()-idx1);
                break;
            } else {
                ans += str2.substr(idx2, str2.size()-idx2);
                break;
            }
        }
        return ans;
    }

    pair<int, int> minCost(const string& str1, const string& str2, int idx1, int idx2) {
        if(idx2 == str2.size()) {
            memo[idx1][idx2] = {(int)str1.size() - idx1, 4};
            return memo[idx1][idx2];
        }
        if(idx1 == str1.size()) {
            memo[idx1][idx2] = {(int)str2.size() - idx2, 5};
            return memo[idx1][idx2];
        }

        if(memo[idx1][idx2].first != INT_MAX) return memo[idx1][idx2];
        pair<int,int> ans;
        
        if(str1[idx1] == str2[idx2]) {
            ans = {minCost(str1, str2, idx1+1, idx2+1).first+1, 3};
        } else {
            int sz1 = minCost(str1, str2, idx1, idx2+1).first;
            int sz2 = minCost(str1, str2, idx1+1, idx2).first;
            if(sz1 < sz2) ans = {sz1+1, 0};
            else ans = {sz2+1, 1};
        }
        memo[idx1][idx2] = ans;
        return ans;
    }

    vector<vector<pair<int,int>>> memo;
};