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 <= stamp.length <= target.length <= 1000
stamp
andtarget
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 <= stamp.length <= target.length <= 1000
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