当前位置:网站首页>leetcode-2337:移动片段得到字符串
leetcode-2337:移动片段得到字符串
2022-07-21 23:16:00 【菊头蝙蝠】
题目
给你两个字符串 start 和 target ,长度均为 n 。每个字符串 仅 由字符 ‘L’、‘R’ 和 ‘_’ 组成,其中:
- 字符 ‘L’ 和 ‘R’ 表示片段,其中片段 ‘L’ 只有在其左侧直接存在一个 空位 时才能向 左 移动,而片段 ‘R’ 只有在其右侧直接存在一个 空位 时才能向 右 移动。
- 字符 ‘_’ 表示可以被 任意 ‘L’ 或 ‘R’ 片段占据的空位。
如果在移动字符串 start 中的片段任意次之后可以得到字符串 target ,返回 true ;否则,返回 false 。
示例 1:
输入:start = "_L__R__R_", target = "L______RR"
输出:true
解释:可以从字符串 start 获得 target ,需要进行下面的移动:
- 将第一个片段向左移动一步,字符串现在变为 "L___R__R_" 。
- 将最后一个片段向右移动一步,字符串现在变为 "L___R___R" 。
- 将第二个片段向右移动散步,字符串现在变为 "L______RR" 。
可以从字符串 start 得到 target ,所以返回 true 。
示例 2:
输入:start = "R_L_", target = "__LR"
输出:false
解释:字符串 start 中的 'R' 片段可以向右移动一步得到 "_RL_" 。
但是,在这一步之后,不存在可以移动的片段,所以无法从字符串 start 得到 target 。
示例 3:
输入:start = "_R", target = "R_"
输出:false
解释:字符串 start 中的片段只能向右移动,所以无法从字符串 start 得到 target 。
解题
写法一:
class Solution {
public:
bool canChange(string start, string target) {
string s=start,t=target;
s.erase(remove(s.begin(),s.end(),'_'),s.end());
t.erase(remove(t.begin(),t.end(),'_'),t.end());
if(s!=t) return false;
for(int i=0,j=0;i<start.size();i++){
if(start[i]=='_') continue;
while(j<target.size()&&target[j]=='_') j++;
if(start[i]=='L'&&i<j) return false;
if(start[i]=='R'&&i>j) return false;
j++;
}
return true;
}
};
边栏推荐
- What if Lao Xue's host disk space is full
- [mysql] basic database operations
- 2022.7.21-----leetcode. eight hundred and fourteen
- Talk about the top 10 mistakes often made in implementing data governance
- One bite of Stream(8)
- 边框动态效果实现
- Interview question 05.07 Pairing exchange
- [mysql] index transactions
- 【PostgreSQL 15】PostgreSQL 15对UNIQUE和NULL的改进
- 2022年数据库审计产品排行榜-必看!
猜你喜欢
Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
It's a holiday. I need to read books carefully
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source
自动化设备制造行业常见管理难题及解决方案
《性能之巅第2版》阅读笔记(五)--Disks监测
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
Coordinate system in QT
ERP系统在元器件贸易企业中的应用
[MySQL must know and know] stored procedure | cursor
617. Merge binary tree
随机推荐
Qscriptengine official documentation
leetcode 792. Number of matching subsequences
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source
自动化设备制造行业常见管理难题及解决方案
[MdSQL]表的增删查改(进阶)
放假了,需要认真读下的书
QScriptEngine官方说明文档
requires_ grad,grad_ FN, the meaning and use of grad 2
Coordinate system in QT
[MySQL]表的增删查改(基础)
"New energy + energy storage" starts from the digital twin, Tupu will make smart power to the extreme
2022/07/20--- convert string to integer; Maximum value of sliding window
It's a holiday. I need to read books carefully
Different accounts are logged in in different windows of the same browser. When the window is switched, the page refresh account is changed to the last login account
Elephant Swap的LaaS方案优势分析,致eToken表现强势
AMBA 2 AHB、AMBA 3 AHB(AHB_Lite)和AMBA 5 AHB协议比较
自动化测试简历编写应该注意哪方面?有哪些技巧?
What if Lao Xue's host disk space is full
Download picture function, full screen function, copy function
One bite of Stream(9)