Greedy suffix
IntuitionFirst we need to store longest suffix match after each location in text.If suffix count reach m we can go full greedy.If not, we start from the left an
# Intuition

First we need to store longest suffix match after each location in `text`.

If `suffix count` reach `m` we can go full greedy.

If not, we start from the left and find first location where 'free' character can be used, `prefix count + 1 + suffix count == m`, while doing that we are tracking `prefix count`.

# Code - Long version
```csharp []
public class Solution {
    public int[] ValidSequence(string text, string pattern) {
        int n = text.Length;
        int m = pattern.Length;
        if (m == 1) return [0];

        int[] suffix = new int[n];
        int count = 0;
                   
        for (int i = n - 1, suffixI = m - 1; i > -1; i--) {
            suffix[i] = count;

            if (pattern[suffixI] == text[i]) {
                count++;
                suffixI--;
            }

            if (count == m) break;
        }

        int[] result = new int[m];

        if (count == m) {
            bool skipped = false;
            int start = 0;

            for (int i = 0; i < m; i++) {
                if (!skipped && pattern[i] != text[start]) {
                    skipped = true;
                    result[i] = start;
                    start++;
                    continue;
                }

                for (int j = start; j < text.Length; j++) {
                    if (text[j] == pattern[i]) {
                        result[i] = j;
                        start = j + 1;
                        break;
                    }
                }
            }

            return result;
        }

        count = 0;

        for (int i = 0, resultI = 0; i < n; i++) {
            if (count + 1 + suffix[i] == m || pattern[resultI] == text[i]) {
                count++;
                result[resultI] = i;
                resultI++;
            }

            if (count == m) return result;
        }

        return [];
    }
}
```

# Code - Short version
```csharp []
public class Solution {
    public int[] ValidSequence(string text, string pattern) {
        int n = text.Length;
        int m = pattern.Length;
        if (m == 1) return [0];

        int[] suffix = new int[n];
        int count = 0;
                   
        for (int i = n - 1, suffixI = m - 1; i > -1; i--) {
            suffix[i] = count;

            if (count < m && pattern[suffixI] == text[i]) {
                count++;
                suffixI--;
            }
        }

        int[] result = new int[m];

        count = 0;
        bool skipped = false;

        for (int i = 0, resultI = 0; i < n; i++) {
            if ((count + 1 + suffix[i] >= m && skipped == false) || pattern[resultI] == text[i]) {
                if (pattern[resultI] != text[i]) skipped = true;
                count++;
                result[resultI] = i;
                resultI++;
            }

            if (count == m) return result;
        }

        return [];
    }
}
```
['C#']