0936. Stamping The Sequence (H)

Stamping The Sequence (H)

题目

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.

题意

给定义一个初始由'?'组成的字符串,用另一个字符串stamp在其上“敲章”,判断能否通过“敲章”来得到目标字符串,并给出相应的步骤。

思路

正向比较难,可以逆向处理,考虑如何使用“反敲章”来让target变回全'?'字符。参考936. Stamping The Sequence - 知乎 (zhihu.com)


代码实现

Java

class Solution {
    public int[] movesToStamp(String stamp, String target) {
        Deque<Integer> stack = new ArrayDeque<>();			// 记录反敲章步骤
        int remain = target.length();					// 剩余待转化字符
        char[] chars = target.toCharArray();
        int m = stamp.length(), n = target.length();
        boolean[] visited = new boolean[n - m + 1];

        while (remain > 0) {
            boolean found = false;

            for (int i = 0; i + m <= n; i++) {
                if (visited[i]) continue;
                int num = unstamp(chars, i, stamp);
                if (num > 0) {
                    remain -= num;
                    stack.push(i);
                    for (int j = 0; j < m; j++) chars[i + j] = '*';
                    found = true;
                    visited[i] = true;
                }
            }

            if (!found) break;
        }

        return remain > 0 ? new int[0] : toArray(stack);
    }

  	// 返回值为能被转化为'*'的字符的个数
    private int unstamp(char[] chars, int start, String stamp) {
        int num = 0;
        for (int i = 0; i < stamp.length(); i++) {
            if (chars[start + i] == '*') {
                continue;
            } else if (chars[start + i] != stamp.charAt(i)) {
                return 0;
            } else {
                num++;
            }
        }
        return num;
    }

    private int[] toArray(Deque<Integer> stack) {
        int[] arr = new int[stack.size()];
        int i = 0;
        while (!stack.isEmpty()) {
            arr[i++] = stack.pop();
        }
        return arr;
    }
}
posted @ 2021-03-31 22:35  墨云黑  阅读(228)  评论(0编辑  收藏  举报