Untitled
unknown
plain_text
10 months ago
1.1 kB
6
Indexable
int n = start.length();
int i = 0, j = 0;
while (i < n || j < n) {
// Skip 'X' in the start string
while (i < n && start.charAt(i) == 'X') {
i++;
}
// Skip 'X' in the result string
while (j < n && result.charAt(j) == 'X') {
j++;
}
// If both pointers reach the end, the transformation is valid
if (i == n || j == n) {
return i == n && j == n;
}
// If the characters at i and j are different, transformation is not possible
if (start.charAt(i) != result.charAt(j)) {
return false;
}
// Check for invalid moves
if (start.charAt(i) == 'L' && i < j) {
return false; // 'L' can't move to the right
}
if (start.charAt(i) == 'R' && i > j) {
return false; // 'R' can't move to the left
}
// Move both pointers forward
i++;
j++;
}
return true;Editor is loading...
Leave a Comment