Track same items counts
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```csharp []\npublic class Solution {\n    public int[] ValidSequence(string word1, string word2) {\n        int len1 = word1.Length, len2 = word2.Length;\n        if(len1 < len2 || len2 == 0) // invalid cases\n            return new int[0];\n\n        int[] res = new int[len2];\n        int[] restSameCnt = new int[len1]; // track the same items Cnt from current to last;\n        int index = len2-1;\n        for(int i = len1-1; i >= 0; i--)\n        {\n            if(index >= 0 && word1[i] == word2[index])\n                index--;\n\n            restSameCnt[i] = len2-1-index;\n        }\n\n        bool replaced = false;\n        index = 0;\n        for(int i = 0; i <= len1; i++)\n        {\n            if(i == len1 || index == len2)\n                return index == len2 ? res : new int[0];\n\n            if(word1[i] == word2[index])\n            {\n                res[index++] = i;\n            }\n            else if(!replaced && (i == len1-1 || restSameCnt[i+1] >= len2-1-index))\n            {\n                res[index++] = i;\n                replaced = true;\n            }\n        }\n\n        return new int[0];\n    }\n}\n```
['C#']