Leetcode 6114.移动片段得到字符串
1.问题描述
2.解决方案
1.记录start和target的L,R的位置,然后如果L+R的数量不一样就直接return false
2.然后一一对比,如果LR的相对位置不一样start.charAt(s)!=target.charAt(t)直接return false,因为相对位置不一样就不可能一动成功
3.对比过程中两个字符一样start的LR必须在targetLR的中间,如果不是就return false,最终return true
class Solution {
public boolean canChange(String start, String target) {
//1.记录
int len = start.length();
if(target.length()!=len) return false;
boolean[][] ints = new boolean[2][len];
int s_sum = 0;
int t_sum = 0;
for(int i=0;i<len;i++){
if(start.charAt(i)=='L'||start.charAt(i)=='R'){
s_sum++;
ints[0][i] = true;
}
if(target.charAt(i)=='L'||target.charAt(i)=='R'){
t_sum++;
ints[1][i] = true;
}
}
if(s_sum!=t_sum) return false;
//2.对比
int s = 0;
int t = 0;
while (true) {
while (s<=len-1&&ints[0][s]==false){
s++;
}
while (t<=len-1&&ints[1][t]==false){
t++;
}
if(s>len-1||t>len-1) break;
if(start.charAt(s)!=target.charAt(t)) return false;
if(start.charAt(s)=='L'&&s<t) return false;
if(start.charAt(s)=='R'&&s>t) return false;
s++;
t++;
}
return true;
}
}