Easy to understand
\n# Code\ncpp []\nclass Solution {\npublic:\n    vector<int> validSequence(string word1, string word2) {\n        int n1 = word1.size(), n2 = word2.size();\n   
\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> validSequence(string word1, string word2) {\n        int n1 = word1.size(), n2 = word2.size();\n        vector<int>mxSuffix(n1 + 1, 0);\n        int sz = 0, ptr = n2 - 1;\n        for(int i = n1 - 1; i >= 0; i--)\n        {\n            if(ptr >= 0 && word1[i] == word2[ptr])\n            {\n                ptr--; sz++;\n            }\n            mxSuffix[i] = sz;\n            \n        }\n        // for(auto &it : mxSuffix)cout<<it<<" "; cout<<endl;\n        sz = 0;\n        vector<int>ans(n2, -1);\n        for(int i = 0, j = 0; i < n1 && j < n2; i++)\n        {\n            if(word1[i] == word2[j])\n            {\n                ans[j] = i;\n                j++; sz++;\n            }\n            else if(mxSuffix[i + 1] + sz + 1 >= n2)\n            {\n                // cout<<"i "<<i<<endl;\n                ans[j] = i; j++; i++; sz++;\n                for(; i < n1; i++)\n                {\n                    if(word1[i] == word2[j])\n                    {\n                        ans[j] = i;\n                        j++; sz++;\n                    }\n                }\n                break;\n            }\n        }\n        if(sz == n2)return ans;\n        return {};\n\n    }\n};\n```
['C++']