Stamping The Sequence

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可以覆蓋char,所以最後一個stamp肯定是full stamp,那麼逆着想問題,我們反着找index就可以了。最後把收集到的index,reverse一下。canreplace判斷以i爲起點,後面的S是否可以被替換,如果不行,直接返回空,否則替換,同時記錄最新替換過程我replace了幾個星星;如果最後星星數目== tt長度,那麼就輸出;

class Solution {
    public int[] movesToStamp(String stamp, String target) {
        char[] ss = stamp.toCharArray();
        char[] tt = target.toCharArray();
        List<Integer> list = new ArrayList<Integer>();
        boolean[] visited = new boolean[tt.length];
        int stars = 0;
        
        while(stars < tt.length) {
            boolean doneReplace = false;
            for(int i = 0; i <= tt.length - ss.length; i++) {
                if(!visited[i] && canReplace(ss, tt, i)) {
                    doneReplace = true;
                    stars += doReplace(ss, tt, i);
                    visited[i] = true;
                    list.add(i);
                }
            }
            
            if(!doneReplace) {
                return new int[0];
            }
        }
        
        int[] res = new int[list.size()];
        int index = 0;
        for(int i = list.size() - 1; i >= 0; i--) {
            res[index++] = list.get(i);   
        }
        return res;
    }
    
    private boolean canReplace(char[] ss, char[] tt, int pos) {
        for(int i = 0; i < ss.length; i++) {
            if(tt[i + pos] != '*' && tt[i + pos] != ss[i]) {
                return false;
            }
        }
        return true;
    }
    
    private int doReplace(char[] ss, char[] tt, int pos) {
        int count = 0;
        for(int i = 0; i < ss.length; i++) {
            if(tt[i + pos] == '*') {
                continue;
            } else {
                tt[i + pos] = '*';
                count++;
            }
        }
        return count;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章