题目:777. 在LR字符串中交换相邻字符
思路:
1. 去掉X后,start是否等于end
2. 采用双指针,因为根据题意,L只能向左移动,R只能向右移动
3. 所以end中的L坐标必须比start中对应的L坐标小,R亦同理
题解:
class Solution {
public:
bool canTransform(string start, string end) {
int n_start = start.size();
int n_end = end.size();
if(n_start!=n_end) return false;
string simStart;
string simEnd;
for(auto x:start){
if(x!='X'){
simStart += x;
}
}
for(auto x:end){
if(x!='X'){
simEnd += x;
}
}
if(simStart!=simEnd) return false;
int li = 0,ri = 0;
while(li<n_start && ri<n_end){
while(li<n_start && start[li]!='L') li++;
while(ri<n_end && end[ri]!='L') ri++;
if(li<ri) return false;
li++;
ri++;
}
li = 0;
ri = 0;
while(li<n_start && ri<n_end){
while(li<n_start && start[li]!='R') li++;
while(ri<n_end && end[ri]!='R') ri++;
if(li>ri) return false;
li++;
ri++;
}
return true;
}
};