In a string composed of 'L', 'R', and 'X' characters, like
"RXXLRXRXL", a move consists of either replacing one occurrence of "XL"
with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string result, return True if and only if there exists a sequence of moves to transform start to result.
Input: start ="RXXLRXRXL", result ="XRLXXRRLX"Output: trueExplanation: We can transform start to result following these steps:RXXLRXRXL ->XRXLRXRXL ->XRLXRXRXL ->XRLXXRRXL ->XRLXXRRLX
We scan both strings, skipping ‘X’. The order and type of ‘L’ and ‘R’ must match, and their allowed movement directions are checked by index. If all checks pass, the transformation is possible.
We use two pointers to scan both strings, skipping ‘X’. For each non-‘X’ character, the order and type must match. ‘L’ can only move left (so its index in start must be >= its index in result), and ‘R’ can only move right (so its index in start must be <= its index in result).
classSolution {
public:bool canTransform(string start, string end) {
int n = start.size(), i =0, j =0;
while (i < n || j < n) {
while (i < n && start[i] =='X') ++i;
while (j < n && end[j] =='X') ++j;
if (i == n && j == n) return true;
if (i == n || j == n) return false;
if (start[i] != end[j]) return false;
if (start[i] =='L'&& i < j) return false;
if (start[i] =='R'&& i > j) return false;
++i; ++j;
}
return true;
}
};
classSolution {
publicbooleancanTransform(String start, String end) {
int n = start.length(), i = 0, j = 0;
while (i < n || j < n) {
while (i < n && start.charAt(i) =='X') i++;
while (j < n && end.charAt(j) =='X') j++;
if (i == n && j == n) returntrue;
if (i == n || j == n) returnfalse;
if (start.charAt(i) != end.charAt(j)) returnfalse;
if (start.charAt(i) =='L'&& i < j) returnfalse;
if (start.charAt(i) =='R'&& i > j) returnfalse;
i++; j++;
}
returntrue;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funcanTransform(start: String, end: String): Boolean {
val n = start.length
var i = 0; var j = 0while (i < n || j < n) {
while (i < n && start[i] =='X') i++while (j < n && end[j] =='X') j++if (i == n && j == n) returntrueif (i == n || j == n) returnfalseif (start[i] != end[j]) returnfalseif (start[i] =='L'&& i < j) returnfalseif (start[i] =='R'&& i > j) returnfalse i++; j++ }
returntrue }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcanTransform(self, start: str, end: str) -> bool:
n = len(start)
i = j =0while i < n or j < n:
while i < n and start[i] =='X': i +=1while j < n and end[j] =='X': j +=1if i == n and j == n: returnTrueif i == n or j == n: returnFalseif start[i] != end[j]: returnFalseif start[i] =='L'and i < j: returnFalseif start[i] =='R'and i > j: returnFalse i +=1; j +=1returnTrue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfncan_transform(start: String, end: String) -> bool {
let n = start.len();
let (mut i, mut j) = (0, 0);
let (s, e) = (start.as_bytes(), end.as_bytes());
while i < n || j < n {
while i < n && s[i] ==b'X' { i +=1; }
while j < n && e[j] ==b'X' { j +=1; }
if i == n && j == n { returntrue; }
if i == n || j == n { returnfalse; }
if s[i] != e[j] { returnfalse; }
if s[i] ==b'L'&& i < j { returnfalse; }
if s[i] ==b'R'&& i > j { returnfalse; }
i +=1; j +=1;
}
true }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
functioncanTransform(start: string, end: string):boolean {
constn=start.length;
leti=0, j=0;
while (i<n||j<n) {
while (i<n&&start[i] ==='X') i++;
while (j<n&&end[j] ==='X') j++;
if (i===n&&j===n) returntrue;
if (i===n||j===n) returnfalse;
if (start[i] !==end[j]) returnfalse;
if (start[i] ==='L'&&i<j) returnfalse;
if (start[i] ==='R'&&i>j) returnfalse;
i++; j++;
}
returntrue;
}