Swap Adjacent in LR String
这题就是看L和R的特性, 有三个特性, L不能往右走,R不能往左走, 去掉X两个string一样.
class Solution {
public boolean canTransform(String start, String end) {
int i = 0;
int j = 0;
int n = start.length();
int m = end.length();
while(i < n && j < m){
if(start.charAt(i) == 'X'){
i++;
}else if(end.charAt(j) == 'X'){
j++;
}else{ // no X
if(start.charAt(i) != end.charAt(j))
return false;
if(start.charAt(i) == 'R' && i > j)
// s[i] == e[j] start = xxR (i == 2) end = xRx (i == 1)
return false;
if(start.charAt(i) == 'L' && i < j)
// s[i] == e[j] start = xLx (i == 1) end = xxL (i == 2)
return false;
i++;
j++;
}
}
while(i < n){
if(start.charAt(i) != 'X') // xxxxLRLRLxxxxxL false
return false;
i++;
}
while(j < m){
if(end.charAt(j) != 'X')
return false;
j++;
}
return true;
}
}