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]
二、分析
该题一定要把它替换的过程多画几遍,然后结合题意,因为题意说,每次盖的印章都是完整的盖下去的,那么你相当于每次都要在target这个串里面找到长度为stamp.length的位置给stamp盖,如果直接思考,会发现不好实现。
所以我们逆向想一下,因为我后盖的章会覆盖掉前面的章,那么我前面的章无论什么时候盖对后盖的章都是没有影响的,所以我先找到最后一次章盖的位置,即在target序列中找stamp,记下盖的序列的最左边位置,然后把相应位置全部替换为‘?’,相当于就是例2中的
"a????ca",然后把这个?看成一个万能的字母,它可以为任意的,再找stamp,这样,一直到最后全部变成target,这些序列就是从最后一次到第一次盖章的位置,然后倒转一下就是题目所要求的。需要注意的是,在写代码的时候,一定要注意需要判断匹配的字符串可能全是?,所以需要防止这种情况产生。
三、代码
class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
vector<int> ans, output;
string aim(target.length(), '?');
while(target != aim)
{
int t = moveAstamp(stamp, target);
if(t == -1)
{
return {};
}
ans.push_back(t);
}
for(int i = ans.size()-1; i >= 0; i--)
output.push_back(ans[i]);
return output;
}
int moveAstamp(string stamp, string &target){
for(int i = 0; i < target.length(); i++)
{
int j = 0, k = i;
bool flag = false;
while(j < stamp.length() && k < target.length() && (stamp[j] == target[k] || target[k] == '?'))
{
if(stamp[j] == target[k])
flag = true;
j++;
k++;
}
if(j == stamp.length() && flag)
{
for(int t = i; t < k; t++)
{
target[t] = '?';
}
return i;
}
}
return -1;
}
};
LeetCode936. Stamping The Sequence的更多相关文章
- [Swift]LeetCode936. 戳印序列 | Stamping The Sequence
You want to form a target string of lowercase letters. At the beginning, your sequence is target.len ...
- 936. Stamping The Sequence
You want to form a target string of lowercase letters. At the beginning, your sequence is target.len ...
- [LeetCode] 936. Stamping The Sequence 戳印序列
You want to form a `target` string of lowercase letters. At the beginning, your sequence is target.l ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架
1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...
- Java-Http
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.IOException; 4 im ...
- 125. Valid Palindrome判断有效的有符号的回文串
[抄题]: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- MySql 获取服务提供的sakila数据库(Example Databases)
关于这个数据库也就是样例数据库,数据库,数据库,最可怕的就是没有数据了,对吧?没有数据你学个什么呀. 可是,没有数据,咱会自己insert,那只能适用于初学者.对于数据库的优化方面的学习,还是有大数据 ...
- Ubuntu 复制 拷贝和自适应屏幕
ubuntu 16.04安装vmtools实测无效!!!!!!11 1.解决VMware workstation与主机的粘贴.复制.文件拖拽问题. 2.解决VMware workstations中Ub ...
- Process management of windows
igfxem.exe进程是正常的进程.是intel家的核显驱动类的进程.核显即"核芯显卡",是指GPU部分它是与CPU建立在同一内核芯片上,两者完全融合的芯片."核芯显卡 ...
- java容器 Map Set List
容器:在java中,如果有一个类专门用来存放其他类的对象,这个类就叫做容器,或者叫集合,集合就是将若干性质相同或者相近的类的对象组合在一起而形成一个整体. boolean add(Object obj ...
- CLR VIA C# 泛型的协变和逆变
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 浏览器访问www.meituan.com过程
“从浏览器输入 xxx 到跳转完成的过程发生了什么”,是一个常见的比较综合的面试题,以下是我查阅了一些资料后总结的,如有错误,还望批评指正.(以美团网为例) 1.在浏览器地址栏输入:meituan.c ...
- Oracle——SQL基础
一.SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定义语言DCL: Data ...