C++ easy to understand backtracking
\n// Time: O(M! * ML)\n// Algo: Backtracking\n// 50/72 tests passed, TLE\n\nclass Solution {\npublic:\n    // M: Number of strigs\n    // L :  combined length o
```\n// Time: O(M! * ML)\n// Algo: Backtracking\n// 50/72 tests passed, TLE\n\nclass Solution {\npublic:\n    // M: Number of strigs\n    // L :  combined length of all strings\n    // Time: O(ML)\n    string strCompress(vector<string>& svec) {\n        string current = svec[0];\n        \n        // Find if the postfix of current string is common with prefix of next string\n        for (int i = 0; i < svec.size() - 1; i++) {\n            int clen = current.length();\n            string next = svec[i + 1]; \n            \n            int j = 1;\n            int max_overlap = 0;\n            \n            // Test for an overlap for all lengths starting 1\n            while (j <= min(current.length(), next.length())) {\n                string postfix = next.substr(0, j); \n                string prefix = current.substr(clen - j, j);\n                \n                if (postfix.compare(prefix) != 0) {\n                    j++;\n                    continue;\n                }\n                \n                max_overlap = max(max_overlap, j);\n                j++;\n            }\n            \n            if (max_overlap == 0) {\n                current += next;\n                continue;\n            }\n            \n            current += next.substr(max_overlap);\n        }\n        \n        return current;\n    }    \n\n    \n    void shortestSuperstringRec(vector<string>& A, string& result, vector<string>& svec, int idx, vector<bool>& visited) {\n        if (svec.size() == A.size()) {\n            string s = strCompress(svec);\n            \n            //cout << s << endl;\n            \n            if (result.length() == 0 || s.length() < result.length())\n                result = s;\n            \n            return;\n        }\n\n        for (int i = 0; i < A.size(); i++) {        \n            if (visited[i] == true)\n                continue;\n            \n            svec.push_back(A[i]);\n            visited[i] = true;\n            shortestSuperstringRec(A, result, svec, idx + 1, visited);\n            // Backtrack\n            svec.pop_back();\n            visited[i] = false;\n        }\n    }\n    \n    string shortestSuperstring(vector<string>& A) {\n        string result;\n        int len = 0;\n        vector<string> svec;\n        vector<bool> visited(A.size(), false);\n        \n        if (A.size() == 1)\n            result = A[0];\n        \n        if (A.size() < 2)\n            return result;\n\n        \n        for (int i = 0; i < A.size(); i++)\n            len += A[i].length();\n        \n        shortestSuperstringRec(A, result, svec, 0, visited);\n        return result;\n    }\n};\n```
[]