当前位置:网站首页>Leetcode-2337: move the fragment to get the string
Leetcode-2337: move the fragment to get the string
2022-07-22 13:33:00 【Chrysanthemum headed bat】
leetcode-2337: Move the fragment to get the string
subject
Here are two strings start and target , The length is n . Each string only By the character ‘L’、‘R’ and ‘_’ form , among :
- character ‘L’ and ‘R’ Represents a fragment , In which the fragment ‘L’ There is only one directly on its left vacancy Only when Left Move , And fragments ‘R’ There is only one directly on its right vacancy Only when Right Move .
- character ‘_’ It means that it can be arbitrarily ‘L’ or ‘R’ The space occupied by the fragment .
If you are moving a string start You can get the string after any number of fragments in target , return true ; otherwise , return false .
Example 1:
Input :start = "_L__R__R_", target = "L______RR"
Output :true
explain : From the string start get target , The following movements are required :
- Move the first clip one step to the left , The string now becomes "L___R__R_" .
- Move the last clip one step to the right , The string now becomes "L___R___R" .
- Move the second clip to the right for a walk , The string now becomes "L______RR" .
From the string start obtain target , So back true .
Example 2:
Input :start = "R_L_", target = "__LR"
Output :false
explain : character string start Medium 'R' The clip can be moved one step to the right to get "_RL_" .
however , After this step , There are no moving clips , So you can't get from the string start obtain target .
Example 3:
Input :start = "_R", target = "R_"
Output :false
explain : character string start The clip in can only move to the right , So you can't get from the string start obtain target .
Problem solving
Writing a :
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;
}
};
边栏推荐
- 直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
- 麒麟系统 QT代码 出现 使用未声明的标识符“MainWindow“
- window 编译生成darknet (cuda11.1+opencv4.5+vs2019)
- 2022年数据库审计产品排行榜-必看!
- leetcode-6119:元素值大于变化阈值的子数组
- Privacy-Preserving Generative Deep Neural Networks Support Clinical Data Sharing
- Digital supply chain management system for intelligent instrument industry: accelerating the transformation of enterprise intelligent supply chain platform
- One bite of Stream(9)
- How does Siemens PLC in the factory control room collect the production data of multiple production lines in a centralized and wireless way?
- 自动化测试简历编写应该注意哪方面?有哪些技巧?
猜你喜欢
MySQL basic functions
ERP系统在元器件贸易企业中的应用
直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
[mysql] basic database operations
Clickhouse installation
Itop-rk3568 development board Debian system function test - wired network test
leetcode-6119:元素值大于变化阈值的子数组
How to install MySQL
SaaS service or privatization service for enterprise digital office?
Elephant Swap的LaaS方案优势分析,致eToken表现强势
随机推荐
Go list 修改元素值
Deep and shallow copy
Qscriptengine official documentation
ideal关于 log显示问号且双击无法打不开的解决方法
win10如何设置锁屏后不熄屏
[mysql] basic database operations
[where can I buy hcip question bank?]
一个合约能存储多少数据?
深浅拷贝
(Applied intelligence-2022) transgait: gait recognition and ensemble transformer based on multimodality
Solve the problem of using external files in Google Labs
深入理解完美哈希
Elemen when clicking, modify the playback index of the walking lantern
放假了,需要认真读下的书
2022年数据库审计产品排行榜-必看!
[advanced C language] learning about flexible arrays
pyhton爬取一、二级网站页面,并将爬取的图片信息保存到本地
[QNX Hypervisor 2.2用户手册]8.7 虚拟I/O(VIRTIO)
[MySQL must know and know] stored procedure | cursor
Chart drawing summary