C++
\nclass Solution {\npublic:\n    vector<int> validSequence(string w1, string w2) {\n        int n = w1.size(), m = w2.size();\n        vector<int> c(n);\n      
```\nclass Solution {\npublic:\n    vector<int> validSequence(string w1, string w2) {\n        int n = w1.size(), m = w2.size();\n        vector<int> c(n);\n        int j = m - 1;\n\n        // Step 1: Fill the `c` array which tracks how many characters can match w2 from the suffix of w1\n        for (int i = n - 1; i >= 0; i--) {\n            if (j >= 0 && w1[i] == w2[j]) {\n                c[i] = (i == n - 1) ? 1 : c[i + 1] + 1;\n                j--;\n            } else {\n                c[i] = (i == n - 1) ? 0 : c[i + 1];\n            }\n        }\n\n        // Step 2: Try to find the valid sequence of indices\n        j = 0;\n        vector<int> ans;\n        int victory = -1;\n        \n        for (int i = 0; i < n; i++) {\n            if (w1[i] == w2[j]) {\n                ans.push_back(i);\n                j++;\n                if (j == m) {\n                    break;\n                }\n            } else {\n                // Check if we can match the remaining part of w2 from the suffix\n                if ((i == n - 1 ? 0 : c[i + 1]) >= m - j - 1) {\n                    ans.push_back(i);\n                    j++;\n                    victory = i + 1;\n                    break;\n                }\n            }\n        }\n\n        // If we already found a full match, return the answer\n        if (ans.size() == m) {\n            return ans;\n        }\n\n        // If no partial sequence was valid, return an empty result\n        if (victory == -1) {\n            return vector<int>();\n        }\n\n        // Step 3: Continue matching the remaining characters of w2 starting from `victory`\n        for (int i = victory; i < n; i++) {\n            if (w1[i] == w2[j]) {\n                ans.push_back(i);\n                j++;\n            }\n            if (j == m) {\n                break;\n            }\n        }\n\n        // Return the answer if we found a valid sequence, otherwise return an empty vector\n        return (ans.size() == m) ? ans : vector<int>();\n    }\n};\n\n```
['C++']