ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

936. Stamping The Sequence

2022-08-22 07:30:50  阅读:158  来源: 互联网

标签:index Stamping target Sequence stamp get length abc 936


You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'.

In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp.

  • For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:
    • place stamp at index 0 of s to obtain "abc??",
    • place stamp at index 1 of s to obtain "?abc?", or
    • place stamp at index 2 of s to obtain "??abc".
    Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s).

We want to convert s to target using at most 10 * target.length turns.

Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 * target.length turns, return an empty array.

 

Example 1:

Input: stamp = "abc", target = "ababc"
Output: [0,2]
Explanation: Initially s = "?????".
- Place stamp at index 0 to get "abc??".
- Place stamp at index 2 to get "ababc".
[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]
Explanation: Initially s = "???????".
- Place stamp at index 3 to get "???abca".
- Place stamp at index 0 to get "abcabca".
- Place stamp at index 1 to get "aabcaca".

 

Constraints:

  • 1 <= stamp.length <= target.length <= 1000
  • stamp and target consist of lowercase English letters.
class Solution {
    public int[] movesToStamp(String stamp, String target) {
        char[] s = stamp.toCharArray();
        char[] t = target.toCharArray();
        int sl = s.length;
        int tl = t.length;
        int star = 0;
        List<Integer> pos = new ArrayList();
        boolean[] visited = new boolean[tl];
        
        while(star < tl) {
            boolean replaced = false;
            for(int i = 0; i <= tl - sl; i++) {
                if(!visited[i] && canreplace(s, t, i)) {
                    star = doreplace(t, i, s);
                    visited[i] = true;
                    pos.add(i);
                    replaced = true;
                    if(star == tl) break;
                }
            }
            if(!replaced) return new int[0];
        }
        
        int[] res = new int[pos.size()];
        for(int i = 0; i < pos.size(); i++) {
            res[i] = pos.get(pos.size() - i - 1);
        }
        return res;
    }
    
    public boolean canreplace(char[] s, char[] t, int p) {
        for(int i = 0; i < s.length; i++) {
            if(t[i + p] != '*' && t[i + p] != s[i]) return false;
        }
        return true;
    }
    
    public int doreplace(char[] t, int p, char[] s) {
        int c = 0;
        for(int i = 0; i < s.length; i++) {
            if(t[i + p] != '*') t[i + p] = '*';
        }
        for(char ch : t) {
            if(ch == '*') c++;
        }
        return c;
    }  
}

https://leetcode.com/problems/stamping-the-sequence/discuss/201546/12ms-Java-Solution-Beats-100

Thinking reversely

标签:index,Stamping,target,Sequence,stamp,get,length,abc,936
来源: https://www.cnblogs.com/wentiliangkaihua/p/16611617.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有