Java Easy to Understand with Pictures
\n\nFrom the above image it can be observed that \n Both start and end have same number of L and R .\n Length of start and end are same.\n XL transforming to LX
![image](https://assets.leetcode.com/users/images/870e1979-2650-4669-bfa0-c29f4d516259_1636616700.965476.jpeg)\n<br/>\nFrom the above image it can be observed that \n* Both start and end have same number of L and R .\n* Length of start and end are same.\n* XL transforming to LX , means it either stays in the same start index  or is less than that of its start index in the end.\n* Similarly RX transforming to XR , means it either stays in the same start index  or is greater than that of its start index in the end.\n\n<br/>\n** JAVA**\n\n```\nclass Solution {\n    public boolean canTransform(String start, String end) {\n\t\n\t//Checking if the number of L and R are equal without X in start and end\n        if(!start.replace("X","").equals(end.replace("X",""))) return false;\n\t\t\n\t//Checking if length of start and end are different\n        if(start.length()>end.length() || end.length()>start.length()) return false;\n        \n        //Making Arraylist for storing indices of L and R in both start and end\n\t\t\n        ArrayList<Integer> lstart = new ArrayList<>(); \n        ArrayList<Integer> lend = new ArrayList<>(); \n        ArrayList<Integer> rstart = new ArrayList<>(); \n        ArrayList<Integer> rend = new ArrayList<>(); \n        \n\t\t//Iterating over start and storing L and R indices in their respective ArrayList\n        for(int i=0;i<start.length();i++){\n            if(start.charAt(i)==\'L\') lstart.add(i);\n            if(start.charAt(i)==\'R\') rstart.add(i);\n\t\t\n\t\t//Iterating over end and storing L and R indices in their respective ArrayList\n        }\n        for(int i=0;i<end.length();i++){\n            if(end.charAt(i)==\'L\') lend.add(i);\n            if(end.charAt(i)==\'R\') rend.add(i);\n\n        }\n        \n        //Taking two pointers and checking the condition which violates the rules\n\t\t\n\t\t//For L\n        int i=0,j=0;\n        while(i<lstart.size() && j<lend.size()){\n            if (lend.get(j)>lstart.get(i)) return false;    //index in start should be less than or equal to index in end \n            i++;\n            j++;\n        }\n        \n\t\t//For R\n        i=0;j=0;\n         while(i<rstart.size() && j<rend.size()){\n            if (rend.get(j)<rstart.get(i)) return false;   //index in start should be greater than or equal to index in end \n             i++;\n             j++;\n        }\n        \n        return true;\n    }\n}\n```
['Java']