[Swift Weekly Contest 109]LeetCode936. 戳印序列 | 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.


你想要用小写字母组成一个目标字符串 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 = "aabcaca", target = "abca"
输出:[3,0,1]

提示:

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


52ms

 1 class Solution {
 2     func movesToStamp(_ stamp: String, _ target: String) -> [Int] {
 3         //转换为数组
 4         var t:[Character] = target.toCharArray()
 5         var s:[Character] = stamp.toCharArray()
 6
 7         var route:[Int] = [Int]()
 8         let num:Int = t.count - s.count + 1
 9         var done:[Bool] = [Bool](repeating:false,count:num)
10         while(true)
11         {
12             var up:Bool = false
13             for i in 0..<num
14             {
15                 if done[i] {continue}
16                 var cor = cors(s, t, i)
17                 if cor == 1
18                 {
19                     route.insert(i, at: 0)
20                     for j in 0..<s.count
21                     {
22                         t[j+i] = "."
23                     }
24                     up = true
25                     done[i] = true
26                 }
27                 else if cor == 2
28                 {
29                     done[i] = true
30                 }
31             }
32             if !up {break}
33         }
34         for i in 0..<t.count
35         {
36             if t[i] != "."
37             {
38                 return []
39             }
40         }
41         var ret:[Int] = [Int](repeating:0,count:route.count)
42         var p:Int = 0
43         for x in route
44         {
45             ret[p++] = x
46         }
47         return ret
48     }
49
50     func cors(_ s:[Character],_ t:[Character],_ f:Int) -> Int
51     {
52         var ex:Int = 0
53         for i in 0..<s.count
54         {
55             if t[i+f] == "." {continue}
56             ex = 1
57             if s[i] != t[i+f] {return 0}
58         }
59         if ex == 0 {return 2}
60         return 1
61     }
62 }
63
64 extension String {
65     //转换为字符数组
66     func toCharArray() -> [Character]
67     {
68         var chars:[Character] = [Character]()
69         for char in self.characters
70         {
71             chars.append(char)
72         }
73         return chars
74     }
75 }
76 /*扩展Int类,实现自增++运算符*/
77 extension Int{
78     //后缀++:先执行表达式后再自增
79     static postfix func ++(num:inout Int) -> Int {
80         //输入输出参数num
81         let temp = num
82         //num加1
83         num += 1
84         //返回加1前的数值
85         return temp
86     }
87 }

原文地址:https://www.cnblogs.com/strengthen/p/9904208.html

时间: 2024-10-05 17:16:07

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence的相关文章

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order 

[Swift Weekly Contest 115]LeetCode960. 删列造序 ||| | Delete Columns to Make Sorted III

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["babca",&quo

[Swift Weekly Contest 118]LeetCode969.煎饼排序 | Pancake Sorting

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort t

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you