JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • algorithm
  • leetcode
JavaInterview.cn
2024-09-22
目录

戳印序列Java

文章发布较早,内容可能过时,阅读注意甄别。

# 题目

你想要用小写字母组成一个目标字符串 target。

开始的时候,序列由 target.length 个 '?' 记号组成。而你有一个小写字母印章 stamp。

在每个回合,你可以将印章放在序列上,并将序列中的每个字母替换为印章上的相应字母。你最多可以进行 10 * target.length 个回合。

举个例子,如果初始序列为 "?????",而你的印章 stamp 是 "abc",那么在第一回合,你可以得到 "abc??"、"?abc?"、"??abc"。(请注意,印章必须完全包含在序列的边界内才能盖下去。)

如果可以印出序列,那么返回一个数组,该数组由每个回合中被印下的最左边字母的索引组成。如果不能印出序列,就返回一个空数组。

例如,如果序列是 "ababc",印章是 "abc",那么我们就可以返回与操作 "?????" -> "abc??" -> "ababc" 相对应的答案 [0, 2];

另外,如果可以印出序列,那么需要保证可以在 10 * target.length 个回合内完成。任何超过此数字的答案将不被接受。

示例 1:

输入:stamp = "abc", target = "ababc"
输出:[0,2]
([1,0,2] 以及其他一些可能的结果也将作为答案被接受)

示例 2:

输入:stamp = "abca", target = "aabcaca"
输出:[3,0,1]

提示:

  • 1 <= stamp.length <= target.length <= 1000
  • stamp 和 target 只包含小写字母。

# 思路

Collections.reverse(ans);

# 解法


class Solution {
    public int[] movesToStamp(String _stamp, String _target) {
        List<Integer> ans = new ArrayList<>();
        char[] stamp = _stamp.toCharArray();
        char[] target = _target.toCharArray();
        while (unStamp(target, stamp, ans)) { }
        if (!isValid(target)) {
            return new int[0];
        }
        Collections.reverse(ans);
        return ans.stream().mapToInt(i -> i).toArray();
    }
    private boolean unStamp(char[] target, char[] stamp, List<Integer> ans) {
        boolean changed = false;
        for (int i = 0; i + stamp.length <= target.length; i++) {
            if (isMatch(target, i, stamp)) {
                markStar(target, i, stamp.length);
                ans.add(i);
                changed = true;
            }
        }
        return changed;
    }
    private void markStar(char[] target, int i, int len) {
        for (int j = 0; j < len; j++) {
            target[i + j] = '*';
        }
    }
    private boolean isMatch(char[] target, int i, char[] stamp) {
        boolean changed = false;
        for (int j = 0; j < stamp.length; j++) {
            if (target[i + j] == '*') {
                
            } else if (target[i + j] == stamp[j]) {
                changed = true;
            } else {
                return false;
            }
        }
        return changed;
    }
    private boolean isValid(char[] target) {
        for (char c: target) {
            if (c != '*') {
                return false;
            }
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 总结

  • 分析出几种情况,然后分别对各个情况实现
微信 支付宝
#Java
最近更新
01
1686. 石子游戏VI Java
08-18
02
1688. 比赛中的配对次数 Java
08-18
03
1687. 从仓库到码头运输箱子 Java
08-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式