Formatted question description: https://leetcode.ca/all/936.html

936. Stamping The Sequence (Hard)

You want to form a target string of lowercase letters.

At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters.

On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns.

For example, if the initial sequence is “?????”, and your stamp is "abc",  then you may make “abc??”, “?abc?”, “??abc” in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)

If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array.

For example, if the sequence is “ababc”, and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves “?????” -> “abc??” -> “ababc”.

Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.

Example 1:

Input: stamp = “abc”, target = “ababc”
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)

Example 2:

Input: stamp = “abca”, target = “aabcaca”
Output: [3,0,1]

Note:

  1. 1 <= stamp.length <= target.length <= 1000
  2. stamp and target only contain lowercase letters.

Solution 1.

From target, try to reverse the process. Find a sequence matching stamp, and erase the sequence with ‘?’. The problem is solvable if and only if we can finally get all ‘?’.

The matching rule:

  1. ’?’ matches all characters
  2. At least one non-‘?’ character should be in the sequence.
// OJ: https://leetcode.com/problems/stamping-the-sequence/

// Time: O(N^2)
// Space: O(1)
class Solution {
private:
    int findStamp(string &stamp, string &target) {
        for (int i = 0; i <= target.size() - stamp.size(); ++i) {
            int j = 0, cnt = 0;
            for (; j < stamp.size(); ++j) {
                if (target[i + j] == '?') continue;
                if (target[i + j] != stamp[j]) break;
                ++cnt;
            }
            if (cnt && j == stamp.size()) return i;
        }
        return -1;
    }
public:
    vector<int> movesToStamp(string stamp, string target) {
        vector<int> ans;
        int cnt = target.size();
        while (cnt) {
            int start = findStamp(stamp, target);
            if (start == -1) return {};
            ans.push_back(start);
            for (int i = 0; i < stamp.size(); ++i) {
                if (target[start + i] != '?') --cnt;
                target[start + i] = '?';
            }
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

Java

class Solution {
    public int[] movesToStamp(String stamp, String target) {
        int stampLength = stamp.length(), targetLength = target.length();
        boolean[] finished = new boolean[targetLength];
        Queue<Integer> queue = new LinkedList<Integer>();
        Stack<Integer> stack = new Stack<Integer>();
        List<Set<Integer>> matchList = new ArrayList<Set<Integer>>();
        List<Set<Integer>> notMatchList = new ArrayList<Set<Integer>>();
        int maxIndex = targetLength - stampLength;
        for (int i = 0; i <= maxIndex; i++) {
            Set<Integer> matchSet = new HashSet<Integer>();
            Set<Integer> notMatchSet = new HashSet<Integer>();
            for (int j = 0; j < stampLength; j++) {
                if (stamp.charAt(j) == target.charAt(i + j))
                    matchSet.add(i + j);
                else
                    notMatchSet.add(i + j);
            }
            matchList.add(matchSet);
            notMatchList.add(notMatchSet);
            if (notMatchSet.isEmpty()) {
                stack.push(i);
                for (int j = 0; j < stampLength; j++) {
                    int index = i + j;
                    if (!finished[index]) {
                        finished[index] = true;
                        queue.offer(index);
                    }
                }
            }
        }
        while (!queue.isEmpty()) {
            int index = queue.poll();
            int startIndex = Math.max(0, index + 1 - stampLength);
            int endIndex = Math.min(maxIndex, index);
            for (int i = startIndex; i <= endIndex; i++) {
                Set<Integer> notMatchSet = notMatchList.get(i);
                if (notMatchSet.remove(index)) {
                    if (notMatchSet.isEmpty()) {
                        stack.push(i);
                        Set<Integer> matchSet = matchList.get(i);
                        for (int j : matchSet) {
                            if (!finished[j]) {
                                finished[j] = true;
                                queue.offer(j);
                            }
                        }
                    }
                }
            }
        }
        for (int i = 0; i < targetLength; i++) {
            if (!finished[i])
                return new int[0];
        }
        int stampSequenceLength = stack.size();
        int[] stampSequence = new int[stampSequenceLength];
        for (int i = 0; i < stampSequenceLength; i++)
            stampSequence[i] = stack.pop();
        return stampSequence;
    }
}

All Problems

All Solutions