1class Solution {2public boolean canTransform(String start, String end) {3int startL = 0, startR = 0;4int endL = 0, endR = 0;5String stLR = "", edLR = "";6for (int i = 0; i < start.length(); i++) {7if (start.charAt(i) != 'X') {8if (start.charAt(i) == 'L') {9startL++;10} else {11startR++;12}13stLR += start.charAt(i);14}15if (end.charAt(i) != 'X') {16if (end.charAt(i) == 'L') {17endL++;18} else {19endR++;20}21edLR += end.charAt(i);22}2324if (startL > endL || startR < endR) // Check conditions at each instant25return false;26}2728if (startL != endL29|| startR != endR30|| !stLR.equals(edLR)) // check their final count and positions31return false;3233return true;34}35}